I am try to fetch data from the database
$check_sql = 'SELECT * FROM table;
$check_result = mysql_query($check_sql);
echo $check_result;
$result = mysql_fetch_array($check_result);
when I echo $check_result, it shows 'Resource id 2', which i think it means there exists a return array, but when I use mysql_fetch_array, it will return a null value, and I don't know why...And I found that no matter whether there exists the resules or not, echo $check_result would always shows 'Resource id#2', does this sentence in mysql mean 'no results' ? Could someone help???
In case if you are dealing with multiple rows in your mysql query you need to use code like this:
while ($row = mysql_fetch_array($check_result) )
{
echo $row['ROW_NAME_HERE'];
}
I guess it is why you mentioned mysql_fetch_array function.
mysql_fetch_array() returns an array.
You definitely must take a look at documentation http://php.net/mysql_fetch_array
Try print_r($result);
Related
I want to select an id from a table using the query:
$demo_id = mysql_result(mysql_query("SELECT id FROM demo_tbl WHERE a_val='a' and b_val='b' LIMIT 1"),0);
This query works perfectly if it satisfies the condition. But if there is no such record then the above mentioned error shows. I dont want to use mysql_num_rows(). Is there any way to find whether $demo_id has value or not.
if(!$demo_id){
//no return
}else{
//demo_id has value
}
base on the manual:
http://php.net/manual/en/function.mysql-result.php
if you are not using mysqli_num_rows() then you try this
if (empty($demo_id)) {
echo 'No results found';
} else {
echo implode($demo_id);
}
and also i mention that don't use mysql_*.. you should use mysqli_*
Mysqli_* manual
I do a query but does not show anything on the screen and when I opened the page where I do the query is slow and does not show anything
$query="SELECT * FROM Dettagli_macchina WHERE macchine_id='$macchine' and Email='$_SESSION[login_user]'";
$result = mysqli_query($conne,$query);
while($row=mysqli_num_rows($result)){
echo $row['COMPONENTE'];
}
mysqli_num_rows() will only use for getting no of rows not for row data.
You need yo use mysqli_fetch_*()
while($row=mysqli_fetch_array($result)){
echo $row['COMPONENTE'];
}
Why this query slow? Because you are using infinite loop here, always TRUE.
while($row=mysqli_num_rows($result))
One more story, I hope you are using session_start() in your file, but suppose that if $_SESSION not found or not start than your query will failed.
In last, this is just a suggestion regarding Naming Convention, you are using column name in small letter, capital small, full capital, this is not related to answer but you must need to learn about this art.
this will help you to understand Naming Convention: Database, Table and Column Naming Conventions?
This reference will help you to understand how mysqli_fetch_array() works: http://php.net/manual/en/mysqli-result.fetch-array.php
Please try with this one for return value and if you need number of raw then the statements will be different.
$query="SELECT * FROM Dettagli_macchina WHERE macchine_id='$macchine' and Email='$_SESSION[login_user]'";
$result = mysqli_query($conne,$query);
while($row=mysqli_fetch_array($result)){
echo $row['COMPONENTE'];
}
As others have pointed out, you have mixed up the mysqli_num_rows function, here I am using it to print the number of results found, then loop through the results after converting the mysqli result object to an array named $row
echo 'Found '. mysqli_num_rows($result) .' results';
while ($row = mysqli_fetch_array($result)) {
echo $row['COMPONENTE'];
}
You are fetching record but using mysqli_num_rows() which return total number of rows/records replace it with mysqli_fetch_array() here is the working example.
$query="SELECT * FROM Dettagli_macchina WHERE macchine_id='$macchine' and Email='$_SESSION[login_user]'";
$result = mysqli_query($conne,$query);
while($row = mysqli_fetch_array($result)) { //replace mysqli_num_rows with mysqli_fetch_array
echo $row['COMPONENTE'];
}
i'm using the following to count the number of rows in my table:
$book_count = query("SELECT status FROM scifi_book WHERE read = $uid");
(count($book_count));
echo $book_count;
and i get the following error:
Notice: Array to string conversion on line 167
(which is the line echo $book_count;)
FIY, the query function is defined and works fine. Never had any issues to insert, select, update and delete.
Am I missing something here?
Try this:
$book_count = query("SELECT status FROM scifi_book WHERE read = $uid");
echo count($book_count);
Also, you need to use print_r($book_count) since your $book_count is not a string.
Suggestion: if you only use that query for getting the count, this may be a bit better:
$book_count = query("SELECT count(*) FROM scifi_book WHERE read = $uid");
your query function seems to return an array, not a string. Instead of echo $follower_count use print_r($follower_count) to see what's inside the query response.
The reason you are seeing that error is because you are echoing Array which is the returned by your query function. echo construct only works on strings, please see the documentation here: http://www.php.net/manual/en/function.echo.php.
Had you used print_r or var_dump then you wouldn't have seen that error. So as #A.S. Roma and Nathaniel Granor suggest use print_r
$book_count = query("SELECT status FROM scifi_book WHERE read =".$uid);
(count($book_count));
echo $book_count;
I want to use PHP to find out if no matches are found. I tried the following, but "is_resource()" always returns true.
$result = mysql_query('...');
if(is_resource($result)){
// Results are found
}
mysql_num_rows() will do the trick.
if (mysql_num_rows($result)>0) {
//Results are found
}
http://php.net/manual/en/function.mysql-num-rows.php
So $result will always be a resource as long as you have proper access to the database. And mysql_num_rows() assumes that the query itself ran successfully. I'd say try something like this:
if($result === FALSE) // Query failed due to not having proper permissions on the table
die('Invalid query: ' . mysql_error());
else if(mysql_num_rows($result) >0)) // We have more than 1 row returned which means we have data
// INPUT RESULTS PROCESSING HERE
else // No rows were returned therefore there were no matches
echo 'No rows returned';
Hope that helps a little =)
Look here for more information if you need: http://www.php.net/manual/en/function.mysql-query.php
This is what you want: mysql_num_rows()
If the query fails it mysql_query will return false so you can check your code like this:
if ( $stmt = mysql_query("...") )
{
// Do some things
}
else
{
// Do some other things
}
Or you could use mysql_num_rows like the people above have stated.
But you should really be looking into MySQLi it's a built in database class. Learn it and use it. Your life will be so much easier.
i'm having a strange problem with php + recordsets.
my code:
$rc = mysql_query("select * from myTable",$db);
if (!$row = mysql_fetch_assoc($rc))
{
$eof=true;
}else{
echo "there is data!<br>";
$rs = mysql_fetch_array($rc);
echo $rs[id];
echo $rs[txt];
}
the strange thing - the query is correct - it's echoing "there is data" but when echoing the actual field values returns empty strings .. :(
any ideas what could be wrong?
In your else block, you are re-fetching some data from the database, using mysql_fetch_array().
But a first row has already been fetched earlier, by mysql_fetch_assoc().
So, basically :
In the if's condition, you are fetching a first row
If it succeed, you enter the else block
where you try to fetch a second row
and using the returned (or not) data, without testing the success of that second fetch.
You should probably do only one fetch -- using either mysql_fetch_assoc or mysql_fetch_array -- but not two.
you already fetched data, use mysql_num_rows() instead
$rc = mysql_query("select * from myTable",$db);
if (mysql_num_rows($rc))
{
echo "there is data!<br>";
$rs = mysql_fetch_array($rc);
echo $rs[id];
echo $rs[txt];
}else{
$eof=true;
}
Be consistent in your use of if mysql_fetch_assoc($rc)...
$rs = mysql_fetch_array($rc);
will return an enumerated array, so $rs['id'] doesn't exist only $rs[0], $rs[1], etc.
Use
$rs = mysql_fetch_assoc($rc);
to return an associative array with $rs['id']
Your first test also fetches and discards a row
And quote the indexes in $rs: $rs['id'] rather than $rs[id]