REPLACE query working in SQL but not in PHP - php

I made the following query:
mysqli_query($conn, "SELECT image_first, REPLACE(image_first,'/home/erik/','')
FROM reviews_media WHERE review_id = $id");
I've tested it in PhpMyAdmin and it's working. But when I echo it, it's still showing the /home/erik/ part.
What am I doing wrong?

Obvioulsy you echo image_first value, but you need to echo result of REPLACE. You can modify query as:
mysqli_query($conn, "SELECT image_first, REPLACE(image_first,'/home/erik/','') as new_img FROM reviews_media WHERE review_id = $id");
See, I added an alias to result of REPLACE function. Now you can echo something like:
echo $row['new_img'];
As you don't do anything to result of REPLACE in a query, you can also simplify it and do a replace with php:
mysqli_query($conn, "SELECT image_first FROM reviews_media WHERE review_id = $id");
// fetching results
echo str_replace('/home/erik/', '', $row['image_first']);

Related

php wont return same as sql query

ok, so I have a search script with is sort of working but I dont get the results I want.
when i run the query in php it doesnt result anything.
$searchStr = htmlspecialchars($searchStr);
$sql = "SELECT * FROM steamitems WHERE steamid='" . $id . "' AND name LIKE '%".$searchStr."%'";
$r_query = mysqli_query($link, $sql);
but if I run the exact same output as $sql (SELECT * FROM steamitems WHERE steamid='76561198196240283' AND name LIKE '%sawed%') in phpmyadmin it returns the correct result..
EDIT: I forgot to mention that I obviously print the results here
while ($row = mysqli_fetch_array($r_query)){
echo $row["assetid"];
echo $row["name];
}

SELECT statement returns first row instead of record that is looked up

I am trying to select a record from my database, and I am return instead the first one in the table. No matter what I try, the first one gets returned.
Here's the query:
$query_task_owner = "select user_id from users where full_name = '$c_task_owner_name'";
$response = #mysqli_query($dbc, $query_task_owner);
Then I try a test to see the value that is returned as such:
echo $response or die(mysql_error());
This is where I see the user_id of the first row.
Even if I try to put a specific value in the query, as follow, I am getting the same result:
$query_task_owner = "select user_id from users where full_name = 'LeBron James'";
I do not understand because when I trying this query directly in PHPMyAdmin, I am getting the right result. So the query itself is correct.
Any idea?
Fetch $response using mysqli_fetch_array().
<?php
$query_task_owner = "select user_id from users where full_name = '$c_task_owner_name'";
$response = #mysqli_query($dbc, $query_task_owner);
$row = mysqli_fetch_array($response,MYSQLI_ASSOC);
echo $row['user_id'];
?>
If, users are more related to that full name. Then, use while loop to fetch all record.
<?php
$query_task_owner = "select user_id from users where full_name = '$c_task_owner_name'";
$response = #mysqli_query($dbc, $query_task_owner);
while($row = mysqli_fetch_array($response,MYSQLI_ASSOC))
{
echo $row['user_id']."<br>";
}
?>

mysql query written using php

$u_id=$event_assoc['Uniqueid'];
echo $u_id."\n";
$result1 = mysql_query("SELECT * FROM eventdetail WHERE unique_id = '$u_id'", $con1);
while($row = mysql_fetch_array($result1))
{
echo 'in eventdetail'."\n";
$e_id= $row['event_id'];
$destination= $row['destination'];
$uniqueid= $row['unique_id'];
$call_num= $row['channelid'];
}
echo mysql_num_rows($result1);
echo $e_id."\n";
echo $destination."\n";
echo $call_num."\n";
echo $uniqueid."\n";
if(mysql_num_rows($result1)>0)
{
echo 'calculate'."\n";
$result= mysql_query("SELECT sum(billsec)
FROM cdr
WHERE uniqueid = '$uniqueid'", $con2);
$bil = mysql_fetch_array($result);
$bill= (float) $bil['sum(billsec)'];
echo $bill."\n";
this is my code..
whenever i try to execute sum function query it retruns top row's billsec instead of addition of all row's billsec
You are doing a where on an unique ID in the sum query. Remove the where and it'll work
You are using unique id in your query.. And in a table single row will have that id, thats why you are getting top row's billsec... Remove the where clause in second query, and then check.
$result= mysql_query("SELECT sum(billsec)
FROM cdr
WHERE uniqueid = '$uniqueid'", $con2);
i guess there is a bug in this area...look again
The PHP statement for the SQL SUM Query should be:-
$result= mysql_query("SELECT sum(`billsec`), `uniqueid` FROM `cdr` GROUP BY `uniqueid` HAVING `uniqueid` = '$uniqueid'", $con2);
You need to use the clause "GROUP BY" whenever you are using any MySQL Aggregate functions (if needed). Also if you are using any aggregate functions (like "SUM", "MAX" etc), then you cannot use "WHERE" clause on the field (in this case, the field is "uniqueid") which is being used in the "GROUP BY" clause.
Hope it helps.

PHP: query, what is wrong?

$BuID= mysql_real_escape_string($_POST['buID']);
$uID= mysql_real_escape_string($_POST['uID']);
$Vn = mysql_query("SELECT id, full_name FROM users WHERE id = '$BuID'");
$vc = mysql_fetch_array($Vn);
$U = mysql_query("SELECT id, full_name FROM users WHERE id = '$uID'"); // WORKS FINE
$showU = mysql_fetch_array($U); // WORKS FINE
The $U/$showU is similiar to the $Vn, $vc above, i have no idea why it wont echo out $vc["id"] and $vc["full_name"], when it do it perfectly with the $U.
echo "<a href='profil.php?id=".$vc[id]."'>e".$vc["full_name"]."</a>"; // Doesnt work
echo "<a href='profil.php?id=".$showU[id]."'>".$showU["full_name"]."</a>"; // Works
The query is syntactically correct, the error may be coming from the escaping of $_POST['buiD'] (is it supposed to be lowercase b?). Your best bet is to echo out the query; if it looks good, manually run it and see if it returns any rows.
Are you sure your $Vn query returns something? Try print_r($vc) after your query and see if you got any data.
When you echo out the two sql statements what do you see?
$BuID= mysql_real_escape_string($_POST['buID']);
$uID= mysql_real_escape_string($_POST['uID']);
$sql = "select id, full_name from users where id = '$BuID'";
$sql2 = "select id, full_name FROM users WHERE id = '$uID'";
echo "<p>$sql</p>";
echo "<p>$sql2</p>";
Check to make sure that the statement with the $BuID variable is being formed correctly.

How to display MySQL Select statement results in PHP

I have the following code and it should return just one value (id) from mysql table. The following code doesnt work. How can I output it without creating arrays and all this stuff, just a simple output of one value.
$query = "SELECT id FROM users_entity WHERE username = 'Admin' ";
$result = map_query($query);
echo $result;
I do something like this:
<?php
$data = mysql_fetch_object($result);
echo $data->foo();
?>
You have to do some form of object creation. There's no real way around that.
You can try:
$query = "SELECT id FROM users_entity WHERE username = 'Admin' ";
//$result = map_query($query);
//echo $result;
$result = mysql_query($query); // run the query and get the result object.
if (!$result) { // check for errors.
echo 'Could not run query: ' . mysql_error();
exit;
}
$row = mysql_fetch_row($result); // get the single row.
echo $row['id']; // display the value.
all you have is a resource, you would still have to make it construct a result array if you want the output.
Check out ADO if you want to write less.
Not sure I exactly understood, what you want, but you could just do
$result = mysql_query('SELECT id FROM table WHERE area = "foo" LIMIT 1');
list($data) = mysql_fetch_assoc($result);
if you wish to execute only one row you can do like this.
$query = "SELECT id FROM users_entity WHERE username = 'Admin' ";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
echo $row[0];
there have been many ways as answered above and this is just my simple example. it will echo the first row that have been executed, you can also use another option like limit clause to do the same result as answered by others above.

Categories