I need to connect to a MySQL database and then show the number of rows. This is what I've got so far;
<?php
include "connect.php";
db_connect();
$result = mysql_query("SELECT * FROM hacker");
$num_rows = mysql_num_rows($result);
echo $num_rows;
?>
When I use that code I end up with this error;
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in C:\Documents and Settings\username\Desktop\xammp\htdocs\news2\results.php on line 10
Thanks in advance :D
You would probably be better of asking the database to aggregate the number of rows instead of transferring them all to php and doing the counting there.
SELECT COUNT(*) FROM hacker
take a habit to run all queries this way:
$sql = "SELECT * FROM hacker";
$res = mysql_query($query) or trigger_error(mysql_error().$sql);
and you will always have comprehensive error information
and take appropriate corrections
also, as it was mentioned above, the only reliable way to count rows is SELECT count(*) query
$sql = "SELECT count(*) FROM hacker";
$res = mysql_query($query) or trigger_error(mysql_error().$sql);
$row = mysql_fetch_row($res);
$count = $row[0];
change your code as following:
$result = mysql_query("SELECT * FROM hacker");
echo mysql_error();
You have an SQL-Error or your not connected to the database
Related
I have a problem with my query and I need to join two tables from different databases now my problem is how can I execute my query. I got my syntax format from here
Please visit first this link so you could understand why my SQL syntax is like this http://www.x-developer.com/php-scripts/sql-connecting-multiple-databases-in-a-single-query
Im using CodeIgniter and here is an Idea of what my query looks like: Notice the way I'm selecting my columns: DATABASE_NAME.TABLE_NAME.COLUMN_NAME
$ENROLLEES = $this->load->database('ENROLLEES', TRUE);
$ACCOUNTS = $this->load->database('ACCOUNTS', TRUE);
$SELECT = "SELECT $ACCOUNTS.BALANCES_TABLE.IDNO, $ACCOUNTS.BALANCES_TABLE.balance";
$FROM = "FROM $ACCOUNTS.BALANCES_TABLE";
$WHERE = "$ACCOUNTS.BALANCES_TABLE.IDNO IN (SELECT $ENROLLEES.ENROLLEES_TABLE.IDNO FROM $ENROLLEES.ENROLLEES_TABLE)";
$SQL = $SELECT ." ". $FROM ." ". $WHERE;
MAIN PROBLEM: How to Execute my query?
If we do like this in codeIgniter:
$ENROLLEES->query($SQL); or $ACCOUNTS->query($SQL);
How can I execute my query that Im having multiple databases? What will I provide here[database]->query($SQL); ?
$sql="Select * from my_table where 1";
$query = $this->db->query($SQL);
return $query->result_array();
If the databases share server, have a login that has priveleges to both of the databases, and simply have a query run similiar to:
$query = $this->db->query("
SELECT t1.*, t2.id
FROM `database1`.`table1` AS t1, `database2`.`table2` AS t2
");
Otherwise I think you might have to run the 2 queries separately and fix the logic afterwards.
I can see what #Þaw mentioned :
$ENROLLEES = $this->load->database('ENROLLEES', TRUE);
$ACCOUNTS = $this->load->database('ACCOUNTS', TRUE);
CodeIgniter supports multiple databases. You need to keep both database reference in separate variable as you did above. So far you are right/correct.
Next you need to use them as below:
$ENROLLEES->query();
$ENROLLEES->result();
and
$ACCOUNTS->query();
$ACCOUNTS->result();
Instead of using
$this->db->query();
$this->db->result();
See this for reference:
http://ellislab.com/codeigniter/user-guide/database/connecting.html
http://www.bsourcecode.com/codeigniter/codeigniter-select-query/
$query = $this->db->query("select * from tbl_user");
OR
$query = $this->db->select("*");
$this->db->from('table_name');
$query=$this->db->get();
return $this->db->select('(CASE
enter code hereWHEN orderdetails.ProductID = 0 THEN dealmaster.deal_name
WHEN orderdetails.DealID = 0 THEN products.name
END) as product_name')
$this->db->select('id, name, price, author, category, language, ISBN, publish_date');
$this->db->from('tbl_books');
I have a simple query which works correctly in phpMyadmin, however doesn't when I echo it out on a page:
$query = "SELECT * FROM PEOPLE WHERE MEMBER_TYPE Like 'LFM' ORDER BY LAST_NAME ASC ";
}
$query = mysqli_query($link, $query);
while ($row = mysqli_fetch_array($query)){...`
For unknown reasons it simply will not sort the results by the last name column from within the table. Any ideas why this could be?
I've been trying to display the number of rows using this code but it keep says
1 Rows which is wrong
<?php
$link = mysql_connect("localhost", "gamin1_forum", "password123");
mysql_select_db("gamin1_forumdb", $link);
$result = mysql_query("SELECT COUNT(*) FROM smf_personal_messages", $link);
$num_rows = mysql_num_rows($result);
echo "$num_rows Rows";
?>
Rows are approximately 1443 but it kept saying 1
The Count(*) returns you one row which contains the number of rows as a value.
By using mysql_num_rows($result) you are actually counting the amount of rows of the Count(*) result which really is one.
Change it to:
$result = mysql_query("SELECT * FROM smf_personal_messages", $link);
$num_rows = mysql_num_rows($result);
Or just use the Count(*) value (which is probably better since it count in the DB and not retrieving the whole table for it) using mysql_fetch_array.
This is true, mysql_num_rows() is telling that there was one "row" returned. You need to check the value of the returned row to get your count. Try this
$link = mysql_connect("localhost", "gamin1_forum", "password123");
$result = mysql_query("SELECT COUNT(*) as cnt FROM smf_personal_messages", $link);
$row = mysql_fetch_array($result);
echo ($row['cnt']);
On a side note, you should not use mysql_* functions for security reasons. Try PDO/mysqli_* functions
I recently use two sql queries to get row record after update like this, for example:
Step 1: I update point for user test#gmail.com
$sql = "UPDATE user SET point=point-:point WHERE email=:email";
$result = $conn->prepare($sql);
$result->bindValue(':point', 2);
$result->bindValue(':email', 'test#gmail.com');
$result->execute();
Step 2: I have to use another sql query to get row['point'] after it is updated
$sql = "SELECT point FROM user WHERE email=:email";
$result = $conn->prepare($sql);
$result->bindValue(':email', 'test#gmail.com');
$result->execute();
$row = $result->fetch(PDO::FETCH_ASSOC);
echo $row['point'];
So, my question is, is ther a shortcut for this? such as 1 single query to achieve this?
Many thanks
I have a column in my MySQL database for deleted values are usually 0 or 1. Usually when doing searches I omit the deleted by doing something like "and deleted = "0"" but I cant figure out how to get this query below to omit my deleted column. any ideas would be appreciated thanks!
$query = "SELECT Status, COUNT(Status) FROM Assets GROUP BY Status";
$result = mysql_query($query) or die(mysql_error());
// Print out result
while($row = mysql_fetch_array($result))
{
echo "". $row['COUNT(Status)'] ." systems ". $row['Status'];
echo ", ";
}
This should work:
$query = "SELECT Status, COUNT(Status) FROM Assets WHERE Assets.deleted = 0 GROUP BY Status";
Also, you should use mysqli or PDO rather than mysql (the php mysql library is obsolete).