Fetching data from mysql data base in php [duplicate] - php

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
I am fetching data from MySQL data base in PHP but it gives error like following:
Warning: mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/content/i/h/u/ihus235/html/cs/emrapp/surveyList.php on line 97
[]
Below is the query which I am using to select data:
$query = mysql_query("SELECT * form survey_Profile where user_Id='".$user_id."' ");

change
$query = mysql_query("SELECT * form survey_Profile where user_Id='".$user_id."' ");
to
$query = mysql_query("SELECT * from survey_Profile where user_Id='".$user_id."' ");

The cause of tthat error can be that mysql returns False.
You can add an:
echo "SELECT * form survey_Profile where user_Id='".$user_id."' ";
To see what string is send to mysql, eventualy test it directly in phpmyadmin.
Also, add this code to see the error from mysql:
if (mysql_errno()) {
echo "<br />". mysql_errno(). " : ". mysql_error(). "<br />";
}

You are getting this error because user_id value doesn't exist into your table.
So before executing your resource into mysql_fetch_assoc(), check whether you got matching rows or not.
if(mysql_num_rows($query) > 0) {
//user mysql_fetch_assoc now
}
And also you have sql syntax error, in your query replace "form" to "from"

Related

selecting data from a table in database php mysql [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 9 years ago.
Trying to get the data from a table in my database and store it into a textfield named "about"
however i keep getting an error:
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, object given in
<?php
require("common.php");
$query = $db->prepare("SELECT * FROM about");
$result = $query or die(mysql_error()); // run the query
$row = mysql_fetch_assoc($result); // fetch a result row
echo $row['about'];
?>
thats because you are not executing your query.after Prepare use Execute().your code will look something like this
<?php
require("common.php");
$query = $db->prepare("SELECT * FROM about");
$query->execute();
$result = $query->fetch(PDO::FETCH_ASSOC);
print_r($result);//to check the elements of the array
echo $row['content'];
?>
Remember
PDO::prepare() - Prepares a statement for execution and returns a statement object
PDOStatement::execute() - Executes a prepared statement
You are missing mysql_query();. This to execute your sql query.
$query = "SELECT * FROM about";
$result = mysql_query($query) or die(mysql_error());

Php Select statement issue with mysql

Php Select statement issue with mysql
I get this error ..
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/try/public_html/register.php on line 17
My Code is
$siteAddress = trim($_POST['b_Address']);
$sql="SELECT * FROM user WHERE siteAddress='$siteAddress';";
$result=mysql_query($sql);
$count=mysql_num_rows($result);
//check for address
if($count)
{
$errorMessage = "<p><font color=red size=4>Site Address " . $siteAddress . " is not available. </font></p>";
$proceed = "no";
}
I try echo $sql and I get this
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource in /home/try/public_html/register.php on line 17
SELECT * FROM user WHERE siteAddress='myshop';
If i input the sql at phpmyadmin it return something..
Showing rows 0 - 0 (1 total, Query took 0.0003 sec)
you have two semi-colons there
$sql="SELECT * FROM user WHERE siteAddress='$siteAddress';";
it should be:
$sql="SELECT * FROM user WHERE siteAddress='" . $siteAddress ."'";
you can do also:
$sql= mysql_query("SELECT * FROM user WHERE siteAddress='" . $siteAddress ."'");
$count=mysql_num_rows($sql);
You could use the count feature of mysql
$count=mysqli_fetch_assoc(mysqli_query($db,"SELECT count(*) as count FROM user WHERE siteAddress='$siteAddress'"))['count'];
or broken down
$query=mysqli_query($db,"SELECT count(*) as count FROM user WHERE siteAddress='$siteAddress'");
$result=mysqli_fetch_assoc($query);
$count=$result['count'];
I've used mysqli in the example as mysql is deprecated and anyone visiting this page may get the impression from the answers that it is still acceptable and safe to use.
try this:
$sql="SELECT * FROM user WHERE siteAddress='{$siteAddress}'";
The curly braces allow PHP to imbed the content of the $siteAddress variable into the string. Also, I don't believe you need the ; at the end of the SQL statement

Warning: mysql_query() expects parameter 2 to be resource [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 1 year ago.
I am currently experiencing issues with the following script. Upon execution of the script, I do recieve the message "Connection was OK!" however, then I also receive the following messages:
Warning: mysql_query() expects parameter 2 to be resource, object
given in /opt/lampp/htdocs/worldofclucky.net/scripts/auth.php on line
11
Warning: mysql_fetch_array() expects parameter 1 to be resource, null
given in /opt/lampp/htdocs/worldofclucky.net/scripts/auth.php on line
12
Any idea what I am doing wrong? I am far from a PHP/MySQL expert, I wouldn't really even consider my self a novice... I did do some testing and the $username variable is sending from the previous page correctly and when typing SELECT * FROM forum.mybb_users WHERE username = 'x_clucky' LIMIT 1 into the MySQL client, it gives all of the information you would expect to get. The PHP code is as follows:
<?php
$username=$_POST["username"];
$hashed_password = md5($_POST['password']); /* For MyBB its $mybb->input['password'] */
$con=mysqli_connect("worldofclucky.net","clucky","CENSORED","forum");
// Check connection
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
} else { echo "Connection was OK!\n";}
$query = mysql_query("SELECT * FROM mybb_users WHERE `username` = '$username' LIMIT 1",$con);
$row = mysql_fetch_array($query);
$encrypted_password = md5(md5($row['salt']).$hashed_password);
if($encrypted_password == $row['password']) {
echo "<script>alert('test');</script>";
}
mysqli_close($con);
?>
Thank you in advanced for your help
change mysql to mysqli and use below kind of query. You can't use mysql and mysqli altogether.
$query = mysqli_query($con, "SELECT * FROM mybb_users WHERE `username` = '$username' LIMIT 1");
$row = mysqli_fetch_array($query);
From a quick look it seems like you are using mysqli functions to connect and then mysql functions to make the actual query. mysql_* functions are now deprecated.

mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in /home/virtua15/public_html/main/1515/dafreg.php on line 9 [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in php
mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource
Im working with two MYSQL databases trying to gahter data from both of them to insert in to field for submission to another database. When i go and use the mysql_fetch_assoc() for the query it gives me an error. I did some research and I cannot find anything that really pertaied to my issue. I was wondering if i could get some help.
<?php
$connect = mysql_connect("localhost","***********","*********") or die ("Couldn't Connect"); //host,username,password
mysql_select_db("virtua15_jo151") or die ("Could not find database");
$query = mysql_query("SELECT jos_comprofiler.firstname, jos_comprofiler.lastname, jos_users.username, jos_comprofiler.cb_country, jos_users.email FROM jos_users LEFT JOIN jos_comprofiler ON jos_users.id=jos_comprofiler.id WHERE jos_users.id = " . mysql_real_escape_string($id));
if ($row = mysql_fetch_assoc($query))
{
$firstname = $row['firstname']; //This is whatever the field is called in your table
$lastname = $row['lastname'];
$loginname = $row['jos_users.username'];
$country = $row['cb_country'];
$email = $row['email'];
}
?>
below this is all HTML I can include that if need be,
Something is wrong with query check with print mysql_error();
Check query. quote properly values assigned in query.
$query = mysql_query("SELECT jos_comprofiler.firstname, jos_comprofiler.lastname,
jos_users.username, jos_comprofiler.cb_country, jos_users.email
FROM jos_users LEFT JOIN jos_comprofiler ON jos_users.id=jos_comprofiler.id
WHERE jos_users.id ='".mysql_real_escape_string($id)."'");

mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result
I' really stuck on this , I'm gettiing this error:
mysql_fetch_assoc(): supplied argument is not a valid MySQL result resource in "filename"
Here is the code:
$sql = "SELECT * FROM $tbl_name WHERE....
$result=mysql_query($sql);
$row = mysql_fetch_assoc($result);
The wierd thing is that I've used the exact same code before and it worked fine
Any ideas??
That means the query failed. Usually it's a SQL syntax error. To find out, just insert this right before the _fetch_assoc line:
print mysql_error();
To prevent the error message, structure your code like this to check the $result beforehand:
$sql = "SELECT * FROM $tbl_name WHERE....";
if ($result = mysql_query($sql)) {
$row = mysql_fetch_assoc($result);
}
else print mysql_error();
Always run all your queries this way
$sql = "SELECT * FROM $tbl_name WHERE....";
$res = mysql_query($sql) or trigger_error(mysql_error()." in ".$sql);
$row = mysql_fetch_assoc($result);
And you will be notified of the cause of error.
But never print or let die() output any errors, as it's security flaw.
This error usually occurs because no data is returned from the query. Make sure the data is being returned by going into something like PHPMyAdmin and making sure the query returns some rows. You should also add the
or die(mysql_error());
At the end of your query.

Categories