I am trying to set the two outputs from this MySQL stored procedure as PHP variables:
$result = mysql_query("CALL mst2('$q', #eset, #leng)");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
while($row = #mysql_fetch_assoc($result))
{
debug($row);
}
$eset = $row->{'#eset'};
$length= $row->{'#leng'};
The last two line are throwing an error Trying to get property of non-object . Does anybody know the proper way to do this?
mysql_fetch_object instead of mysql_fetch_assoc should fix your query up.
Secondly though you should really look at either using mysqli_ or pdo statements.
Links here:
http://php.net/manual/en/pdo.query.php
http://php.net/manual/en/mysqli.query.php
Here's how I got it to work with mysql_query:
$result = mysql_query("CALL mst2('$q', #eset, #leng)");
$result = mysql_query("SELECT #eset, #leng");
if (!$result) {
die('Invalid query: ' . mysql_error());
}
while($row = #mysql_fetch_object($result))
{
$eset = $row->{'#eset'};
}
right after the procedure I called a SELECT statement, then in the while loop, the $eset variable gets set properly.
Related
This question already has answers here:
Object of class mysqli_result could not be converted to string
(5 answers)
Closed 1 year ago.
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('censored','censored','censored','db');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"db");
$sql="SELECT message FROM messages WHERE code = '".$q."'";
$result = mysqli_query($con,$sql);
$resultstring = (string)$result;
echo $resultstring;
mysqli_close($con);
?>
I am attempting to echo the result of the query to the user but when this PHP runs through ajax I get this error:
Recoverable fatal error: Object of class mysqli_result could not be
converted to string in D:\xampp\htdocs\getmessage.php on line 12
Now I don't understand this because I am already converting $result into a string.. Thanks!
mysqli_query() can not convert to string.
You must use mysqli_fetch for parse to array.
Example:
if ($result=mysqli_query($con,$sql))
{
// Fetch one and one row
while ($row=mysqli_fetch_row($result))
{
printf ("%s (%s)\n",$row[0],$row[1]);
}
}
First of all mysqli_query Performs a query on the database
it does not directly return a value
$query = mysqli_query($con,$sql);
so to do that use mysqli_fetch_assoc it will Fetch a result row as an associative array
$result = mysqli_fetch_assoc($query);
Then you can now get your value
$resultstring = $result['message'];
so your code should be like this
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('censored','censored','censored','db');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"db");
$sql="SELECT message FROM messages WHERE code = '".$q."'";
$query = mysqli_query($con,$sql);
$result = mysqli_fetch_assoc($query);
$resultstring = $result['message'];
echo $resultstring;
mysqli_close($con);
?>
you need to convert the data(RS) into array !
$result = mysqli_fetch_assoc($result);
print_r($result);
As an result of your query on success You will get mysqli_result object. If You want to get received rows use fetch data function.
$result = mysqli_query($con,$sql);
$resultstring = $result->fetch_row()[0];
See docs: mysqli_query &
mysqli_result
I have resolved similar issue with following approach.
// Assign your sql statement
$sqlMasterSiteIds = "SELECT DISTINCT table_name FROM site WHERE gwid = 39 AND master_site_id NOT LIKE '0'";
// Run the query
$masterSiteIdsQuery = $this->db->query($sqlMasterSiteIds);
// Prepare container
$masterSiteIds = [];
// Fetch first row of query results
$first_row = $masterSiteIdsQuery->fetch_assoc();
// Assign first row to your prepared container
$masterSiteIds= $first_row['master_site_id'];
// Change the results into string comma separated so it can be used in next mysqli query or echoed
while($sub_row = $masterSiteIdsQuery->fetch_assoc()) {
$masterSiteIds = implode(',', array($masterSiteIds, $sub_row['master_site_id']));
}
// Now you can use $masterSiteIds which is a STRING of values comma separated
echo $masterSiteIds; // Output: '1024, 1142, 8492'
I hope it will help you or anybody else in need. I am not sure if it is the best approach but it worked well in my case as I had to change my results into STRING so I could use it in my next mysqli query with FIND_IN_SET command.
Happy coding!
So i have here a function that i created. It worked when i used a postgres database but once i switched over to mysqli, for some reason, it doesn't return the id. Here is my function:
function getAccID($connection, $username){
$res = mysqli_query($connection, "SELECT accounts_id FROM people WHERE people.username ='$username'");
if ($res){
$row = mysqli_fetch_assoc($res);
if ($row){
return ($row[0]);
}
}else{
//handle the error
}
}
And here is how i declare the function:
$accountID = getAccID($connection, $user);
$p->addContent("Account ID: $accountID");
if (!$accountID) $p->addContent('Error: ' . mysqli_error($connection) . "<br>");
PS: If anyone says anything about me using mysqli_query instead of prepare statements, i will murder some babies :L. I'm using it to test, and once everything works, it's easy to switch to prepared statements.
FIXED
Problem with this was with Postgres i could use the id of the fields, but with mysqli, i have to use the name.
Therefore this will not work
$row[0];
It'll have to be:
$row["name_of_field"];
As you are using mysqli_fetch_assoc
the $row should have the name of the field
use $row['id'] instead of $row[0]
Warning: mysql_fetch_assoc() expects parameter 1 to be resource, boolean given
my code php is :
function getResourceLevel($vid) {
$q = "SELECT * from " . TB_PREFIX . "fdata where vref = $vid";
$result = mysql_query($q, $this->connection);
return mysql_fetch_assoc($result);
}
Please Help me
It looks like mysql_query() has returned false.
This could by due to an error in your mysql syntax or due to a lack of permissions for accessing the database table you have requested.
In either case, you can try calling mysql_error() which will return a string with a best guess of what went wrong with your last mysql function.
Edit: As mentioned in some of the comments, the use of mysql_* functions is discouraged, so if you have opportunity you should update your code to use the mysqli or PDO MySQL extensions. Better yet, use something like Zend DB to move you one layer further away from the database API.
Try to echo your query and run in some mysql query browser and check if its giving required results.
function getResourceLevel($vid) {
$q = "SELECT * from " . TB_PREFIX . "fdata where vref = $vid";
echo $q;
$result = mysql_query($q, $this->connection);
return ($result) ? mysql_fetch_assoc($result) : false;
}
Try this
function getResourceLevel($vid) {
$q = "SELECT * from " . TB_PREFIX . "fdata where vref = $vid";
$result = mysql_query($q, $this->connection) || die(mysql_error());
return mysql_fetch_assoc($result);
}
Notice the || die(mysql_error()) statement.
This will stop the code and show the sql error that is being returned as false.
Hope this helps.
I'm looking to label a string based on database results. I have the following code:
$loc1 = "A";
$query = "SELECT loc FROM User where nick='".$users[$j]."'";
$loc = mysql_query($query);
if($loc == 1)
$loc1 = "A";
if($loc== 2)
$loc1 = "B";
The loc1 location is always "A". I've confirmed that the query works in MySQL and I'm having a difficult time understanding why this simple case is not working. I would appreciate your help!
The function mysql_query does not return the value of the column fetched, instead it returns a MySQL resource which you use to extract the column value returned by the query.
Change:
$loc = mysql_query($query);
to:
$result = mysql_query($query);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
if ($row = mysql_fetch_assoc($result)) {
$loc = $row["loc"];
}
You are storing mysql_query() return value in $loc which is a resource.
For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error.
So, $loc is not 1 or neither equals to 2.
You need to use mysql_fetch_array / mysql_fetch_object, etc to manipulate the resource.
You must fetch the recordset with, in example mysql_fetch_row
Example #1 Fetching one row with mysql_fetch_row()
<?php
$result = mysql_query("SELECT id,email FROM people WHERE id = '42'");
if (!$result) {
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result);
echo $row[0]; // 42
echo $row[1]; // the email value
?>
But, be careful with SQL injection attacks!
http://es.php.net/manual/en/security.database.sql-injection.php
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())
}