I am trying to stop the mysql_query error from being output onto my form. Currently if there is no location found, I receive the error
"Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 11"
I am trying to catch this error, and instead assign the $location variable to not found. My code for attempting this is below, what am I doing wrong?
Thanks!
$query3 = "SELECT `location` FROM location WHERE vin = '$vin' LIMIT 1";
$result3 = mysql_query($query3);
if (!mysql_result($result3,0)) {
$location = "Not found";
} else $location = mysql_result($result3,0,0);
mysql_result() generally shouldn't be used. You'd be better off with something like:
$result3 = mysql_query($query3) or die(mysql_error());
if (mysql_numrows($result3) == 0) then
$location = "not found";
} else {
$row = mysql_fetch_array($result3);
$location = $row[0];
}
Your error is caused by the fact that the query returned no rows - e.g. nothing matched. You then tried to retrieve the first field in the first row of that result set, a row which doesn't exist. Checking the number of returned rows with mysql_numrows() is safer, as that works whether the query found nothing or a bajillion rows.
You should look into how to set your error and warning levels in php ini - usually you want a s little output on prod as possible.
http://www.php.net/manual/en/errorfunc.configuration.php#ini.error-reporting
However, here, the code that would generate that error is:
$result3 = mysql_query($query3);
That is the line you should be writing your if or "or die" statements around:
$result3 = mysql_query($query3)or die($location = "not found");
You should look into using OOP; using a database class to handle interaction with your DB.
But, basically you want to check if there are any rows, before trying to bring back the results.
Try checking with "mysql_num_rows" in your "if" statement:
if (!mysql_num_rows($result3)) {
First, you can add #:
if (!#mysql_result($result3,0)) {
$location = "Not found";
} else $location = mysql_result($result3,0,0);
Second, you can check mysql_num_rows(result3) before mysql_result call.
Mysql_query returns false if nothing is found so a simple :
$result3 = mysql_query($query3);
if (mysql_affected_rows($result3) == 0) {
$location = "Not found";
} else $location = mysql_result($result3,0,0);
Should do it.
Related
I have code
$email = "jb#tlb.com";
$row = mysql_query("SELECT EXISTS(SELECT email FROM accounts WHERE email='".$email."');");
echo $row[0];
However, nothing is echoed.
This is strange because something is being returned because, later in the code I have a function that is CALLED:
if ( $row[0] == 0 ) { echo "Email address is available<br>";};
However: this is strange because when i put the SAME CODE into mySQL database command prompt:
It clearly returns 1 or TRUE.
The mysql_query is returning 0 when the same exact command in mysql command prompt returns 1. Further: I am unable to echo the result for debugging purposes.
EDIT: Please not, the regular mySQL command is returning this and ONLY this:
EDIT: Here is there entire database:
MySQL query gives you a ressource. After that you have to fetch the data with mysql_fetch_assoc or mysql_fetch_row or something else for example. But its better to use prepared statements with mysqli or PDO to get more security.
$email = "jb#tlb.com";
$res = mysql_query("SELECT EXISTS(SELECT email FROM accounts WHERE email='".myql_real_escape_string($email)."')");
$row = mysql_fetch_assoc($res);
echo $row['email'];
Answer to your question:
$email = "jb#tlb.com";
$res = mysql_query("SELECT email FROM accounts WHERE email='".mysql_real_escape_string($email)."')");
$numRows = mysql_num_rows($res);
if($rowRows > 0) {
echo "Record Available";
}
You need to actually retrieve the result set from the query. mysql_query() just returns a resource handle for a successful select. You then need to fetch the results using mysql_fetch_* class of functions. Alternatively, you can use mysql_num_rows() to determine the number of rows returned in the result set.
In this case it is really senseless to wrap your actual query into a subquery. Just run your select and determine the number of rows:
$email = "jb#tlb.com";
$result = mysql_query("SELECT email FROM accounts WHERE email='".$email . "'");
if($result) {
$row_count = mysql_num_rows($result);
echo $row_count;
}
Also, you should not be writing new code using mysql_* functions, as these are deprecated. I would suggest mysqli or PDO extensions instead.
You need to do something like
while ($r = mysql_fetch_assoc($row))
{
echo $r[0];
}
after that code.
Let me know.
I'm trying to check if a specific link is already contained in a database,
but I keep getting an error stating "mysql_num_rows() expects parameter 1 to be resource"
I've tried changing a lot of things, but nothing seems to work. Can anybody help?
$result = mysqli_query($con, "SELECT * FROM `songs` WHERE `link` = '$link'");
if($result == False){
"echo f3";
return False;
}
$count =mysql_num_rows($result);
if($count > 0){
echo "f4", $count;
return False;
}
mysqli_* is not the same as mysql_*. You can't use resource from one in another.
Use mysqli_num_rows() to get number of rows from mysqli resource.
I'm trying to check if a row exist in my db like this:
$uid = $_GET['queryString'];
if(isset($_GET['queryString'])) {
echo "New: ".$uid."<BR>";
$query = "SELECT * FROM stuff WHERE $uid";
// Escape Query
$queryE = $db->real_escape_string($query);
$results = $db->query($queryE);
if(($results->num_rows) > 0) {
echo "NO!";
}
else
{
echo "Make new row";
}
}
else
{
echo 'Error!';
}
But I keep getting the error: Trying to get property of non-object in ....
So if it exist I do one thing, if it doesn't I do the other, i've been searching for about an hour to find the cause, maybe I'm mixing up old PHP4 with my PHP5 stuff?
I've tried a lot, tried some examples but tend to get the error: mysql_fetch_array() expects parameter 1 to be resource, string given
Or should I check it in the query itself?
What line is the error on?!
If it is on the real_escape_string line, you haven't instantiated $db properly.
If it is on the $results->num_rows line, try changing
$results = $db->query($queryE); to:
if(!($results = $db->query($queryE))) {
echo 'Make new row';
} else {
// user likely exists, check $results
}
Also, you need to clean up the way you check the query string -- just process the query variable you want, rather than the whole string. Suggest also to use a different name for the query variable and the table column.
The error I get:
...mysql_fetch_array() expects parameter 1 to be resource, boolean given...
awayid is in the address bar properly. I can print it out just fine, but for some reason the following code gives me the above error.
$result = mysql_query("select * from team where id=" . $_GET['awayid']);
$row = mysql_fetch_array($result);
EDIT Tried the mysql_error(). It seems I forgot to select a database... however, even why I use mysql_select_db('gamelydb'); I still get the mysql error No database selected
Your query is failing... Therefore $result is set to false.
$result = mysql_query("select * from team where id=" . $_GET['awayid']);
var_dump($result); // bool(false)
Call mysql_error() to get the error message for your query:
echo mysql_error();
Your query is failing and returning a boolean FALSE. Try this:
$result = mysql_query("select ...") or die(mysql_error());
^^^^^^^^^^^^^^^^^^^^^^---- add this
This will kill the script and show you the exact reason the query is failing.
mysql_query() returns false if the query is unsuccessful, i.e. an error occured. That is why you need to check $result for being false first.
Use mysql_error() to output the error.
You need to be sure there is results from your query :
while ($row = mysql_fetch_array($result)) {
// echo $row[] ... ;
}
First of all, your query is very open to SQL injection attacks. Do not directly insert anything from $_GET or $_POST (or really anywhere) into your query. At the minimum, use mysql_real_escape_string on the variable.
mysql_query is returning false becuase there is something wrong with the query. You can use mysql_error to see what the last reported error is.
if ($result = mysql_query("select * from team where id='" . $_GET['awayid']) . "'") {
$row = mysql_fetch_array($result);
}
else {
echo mysql_error();
}
Anyway...you know that writing a $_GET parameter right into the SQL query is very very bad? Try it with PHP Data Objects.
Did you try and search around first Tory, we answer these questions over and over again, next time please search around.
The reason why this error occurs is because your running a query with mysql_query that fails, because it fails it returns false, you then pass the value of false to mysql_fetch_array, it's like doing mysql_fetch_array(false)
You need to make sure that mysql_query is successful:
try something like this:
if(false !== ($result = mysql_query("select * from team where id=" . $_GET['awayid'])))
{
$row = mysql_fetch_array($result);
}else
{
die("Query has failed: " . mysql_error())
}
I am grabbing the time from a list of ids. Not every id has a time associated with it.
How can I tell if there are no results (EG ID is not in the table yet) and then not get lots of Warning: mysql_result() [function.mysql-result]: Unable to jump to row 0 on MySQL result index 9 in /home/aslum/... errors...
$tquery = "SELECT time ".
"FROM clicks ".
"WHERE id LIKE '".$id."'";
$tresults = mysql_query($tquery) or die (mysql_error());
// What do I do here?
// IF $TRESULTS == "" THEN $foobar = false else $foobar = true
$clicktime = mysql_result($tresults,0);
if ($foobar==true) echo $clicktime."<br>";
if(mysql_num_rows($result) > 0) {
// do something
}
Here's a slightly more verbose way to see where things are going wrong, using mysql_errno and mysql_num_rows to provide more information.
$sh = mysql_query($sql);
if($sh === false) {
$error = mysql_errno();
if($error) {
// You really should be logging this instead of just calling die.
die(mysql_error());
}
// Otherwise, the query just didn't return a result set for some reason...
echo "Something bad and unexpected happened.";
}
// Now the fun part.
if(mysql_num_rows($sh) > 0) {
echo "I got rows!";
} else {
echo "I got nothin' :(";
}
(Also, please use the mysqli or PDO extensions instead of mysql if you can.)