I am trying to retrieve a blob from a mysql database. However, my query always returns 1, and does not trigger an error. I've been trying the things I found here and there, but I can never get it to return a resource. I manually checked that the blob to be retrieved, so that does not seem to be the problem. Could someone please help me?
$id = 2;
$sqlFetch = "SELECT * FROM mallampati_images WHERE img_id = $id";
$sth = $wpdb->query($sqlFetch) or die ('query failed');
$a = mysql_num_rows($sth);
causes the error:
Warning: mysql_num_rows() expects parameter 1 to be resource, integer given in /home/clients/4ceaa5faab208e8af4350138684e6d6d/web/wp-content/themes/twentytwelve/functions.php on line 539
NULL
this will always return 1.
$sth = $wpdb->query($sqlFetch) or die ('query failed');
what you actually want is
if (false === ($sth = $wpdb->query($sqlFetch)) {
die ('query failed');
}
... rest of code
Here's what I did. It gets a single cell out of a single row, but there are other wordpress functions available, such as $wpdb->get_rowinstead of $wpdb->get_var. It is in fact really simple, but I did not understand the $wpdbAPI at all.
$sth = $wpdb->get_var( $wpdb->prepare( "SELECT img_blob FROM mallampati_images WHERE img_id = 1 ", OBJECT ) );
echo '<img src="data:image/jpeg;base64,'.base64_encode( $sth ).'"/>';
Related
I am trying to update only one column of MySQL table using the code below, but for some reason it always fails and gives me Invalid query error! What am I doing wrong here?
$query = "UPDATE dbTester SET username ='$MediaUserName' WHERE ID = '".$row['ID']."'";
$result2 = mysql_query( $con, $query );
echo $query;
if (!$result2) {
die('Invalid query: ' . mysql_error());
}
Error:
UPDATE dbTester SET username ='cindy' WHERE ID = '10796'Invalid query:
The error occurs because mysql_query expects first argument to be your query in string format and an optional second argument to be connection resource.
resource mysql_query ( string $query [, resource $link_identifier = NULL ] )
you should use, mysql_query($query); instead of mysql_query( $con, $query );
Your syntax suits for mysqli_query($con, $query );
I have been on the site for some time now and I can't seem to get the idea that most of the similar questions are getting answers for this error:
Catchable fatal error: Object of class mysqli could not be converted to string
Saying it is an object. I am fairly new to PHP and it would really help if anyone could explain this to me. I am trying to retrieve data from my database and echo it in a table.
This is what I have done so far:
$dbcon=mysqli_connect("localhost","root","","technoage");
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$results = mysql_query("SELECT * FROM items WHERE item_id = 1,$dbcon");
if(!$results)
{
die("Database query failed".mysql_error());
}
while($row = mysql_fetch_array($results))
{
echo $row['descreption']." ".$row['price']."<br/>";
}
You are connected with mysqli
but your are querying with mysql
$results = mysql_query("SELECT * FROM.....
rewrite your code with mysqli not mysql as you are mixing mysqli with mysql.
this
$results = mysql_query("SELECT * FROM items WHERE item_id = 1,$dbcon");
should be
$results = mysqli_query($dbcon, "SELECT * FROM items WHERE item_id = 1");
and this
while($row = mysql_fetch_array($results))
should be
while($row = mysqli_fetch_array($results))
This error is not caused by mixing mysql and mysqli. It's caused by inserting $dbcon into your SQL string itself, here:
$results = mysql_query("SELECT * FROM items WHERE item_id = 1,$dbcon");
// ^ here
The connection should be passed as the first argument with the query as the second. You're inserting it into the SQL string and passing it as a single argument, which means PHP tries to convert the object to a string.
Change to:
$results = mysqli_query($dbcon, "SELECT * FROM items WHERE item_id = 1");
As others point out, you need to change all mysql_ references to mysqli_ but this is your secondary problem. The immediate problem is the string issue and once that's fixed, you will encounter the mixing mysql problem.
I need help figuring out why the following DB Query is not working. I know the DB connection is good. I also know the $referralname = $_SESSION['user_name']; is correctly rendering. It has to be something with my code.
I am getting the following errors. Maybe this will help to figure this out.
[12-Jun-2013 21:13:54 America/New_York] PHP Warning: mysql_query() expects parameter 1 to be string, object given in /x/x/public_html/americansolar/partner/classes/Referral.php on line 89
[12-Jun-2013 21:13:54 America/New_York] PHP Warning: mysql_num_rows() expects parameter 1 to be resource, null given in /x/x/public_html/americansolar/partner/classes/Referral.php on line 90
P.S. I am not sure if the while statement is necessary or not since it will always only return one result???
My Code:
// creating a database connection
$this->db_connection = new mysqli(DB_HOST, DB_USER, DB_PASS, DB_NAME);
// if no connection errors (= working database connection)
if (!$this->db_connection->connect_errno) {
$referralname = $_SESSION['user_name'];
// get the referrer's id
$query_get_referral_id = $this->db_connection->query("SELECT * From users WHERE user_name = '".$referralname."';");
$result = MYSQL_QUERY($query_get_referral_id);
$numberOfRows = MYSQL_NUM_ROWS($result);
$i = 0;
while ($i<$numberOfRows)
{
$thisId = MYSQL_RESULT($result,$i,"user_id");
$i++;
}
}
My Solution:
$query_get_referral_id = $this->db_connection->query("SELECT * From users WHERE user_name = '".$referralname."';");
while($row = mysqli_fetch_array($query_get_referral_id))
{
$thisId = $row['user_id'];
}
Youre mixing mysqli and mysql... they are two completely different and incompatible interfaces. Secondly, your $query_get_referral_id is not an id value... it is a mysqli_result object. You need to then extract the value from that object.
And lastly... DONT use mysql... stick with mysqli, or use PDO
Also you should use a prepared statement for this:
$stmt = $this->db_connection->query("SELECT user_id From users WHERE user_name = ?");
$stmt->bind_param('s', $referralname);
$stmt->execute();
if($stmt->num_rows) {
$stmt->bind_result($userId);
while($stmt->fetch()) {
// do something with $userId...
// each iteration of this loop is a
// row of the result set, it will automatically
// load the value of the user_id into $userId
}
}
I donot think you should query like
$query_get_referral_id = $this->db_connection->query("SELECT * From users WHERE user_name = '".$referralname."';");
$result = MYSQL_QUERY($query_get_referral_id);
Well, you should go
$result = $this->db_connection->query("SELECT * From users WHERE user_name = '".$referralname."';");
I am playing around with this trying to pull some articles from a database.
Here is the code i have so far: ($host etc are defined in config)
include ("admin/config.php");
$mysqli = mysqli_connect($host, $user , $pass , $database);
$query = "SELECT * FROM articles ORDER BY date LIMIT 5";
$result = mysqli_query($mysqli,$query);
while($row = mysqli_fetch_array($result))
{
Some code to display results
}
This is the error that gets generated but i just cant see what i am doing wrong. Any help will be much appreciated.
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, boolean given in /website....
there is error in your query.
to get it always run all your queries this way
$result = mysqli_query($mysqli,$query) or trigger_error($mysqli->error."[$query]");
I am encountering an error. My log is as follows:
PHP Warning: mysql_num_rows(): supplied argument is not
a valid MySQL result resource in
/home/domain/public_html/index.php on line 96
My code is as followsL
line 96:
mysql_num_rows(mysql_query("SELECT * FROM `table` WHERE
UNIX_TIMESTAMP(`date`)>$user_last_visit"));
What could cause this error?
You put two functions into each other directly:
mysql_num_rows(mysql_query(...));
Technically this is possible to do (function 1 returns something that becomes the parameter of function 2), however, in case function 1 returns something that function 2 can not deal with, you get errors.
That happens in your case. Instead store the return value into a variable and do error handling your own:
$result = mysql_query(...);
if (!$result) {
throw new Exception(sprintf('Database query failed: %s', mysql_error()));
}
$num_rows = mysql_num_rows($result);
Proper error handling is crucial for solid programming, so take care about return values. When in doubt double-check for the function in question with the PHP manual which is really a great resource. See mysql_query.
I would try rearranging the code
$sql = "SELECT * FROM `table` WHERE UNIX_TIMESTAMP(`date`) > $user_last_visit";
$result = mysql_query($sql) or die(mysql_error());
echo mysql_num_rows($result);
I can't tell from your code but you are creating a connection to the mysql server?
If you only want to count the rows, use the COUNT(*) keyword in the query; otherwise, the data base will prepare the whole resulting rows for output although you don't need them.
SELECT COUNT(*)
FROM `table`
WHERE UNIX_TIMESTAMP(`date`) > $user_last_visit
Then, simple execute the query:
$result = mysql_query($query);
list($count) = mysql_fetch_array($result);
Anyway, your query throws an error; there should be another warning showing the error. You can use mysql_error() to find out the error otherwise.
According to http://php.net/manual/en/function.mysql-query.php mysql_query returns "For SELECT, SHOW, DESCRIBE, EXPLAIN and other statements returning resultset, mysql_query() returns a resource on success, or FALSE on error."
So try doing
$query = "SELECT * FROM `table` WHERE UNIX_TIMESTAMP(`date`)>$user_last_visit"
$resource = mysql_query($query) or die("Error: ". mysql_error(). " with query ". $query);
mysql_num_rows($resource);
And then see what happens.