This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 5 years ago.
I am running a PDO query on a MySQL database and I get an error saying there is invalid argument supplied for the foreach. On the frontend, I just pass a string to $questionTable and an integer for $questionID.
What am I doing wrong?
$query = $this->dbConnection->query("SELECT * FROM ('$questionTable') WHERE id = ('$questionID')");
foreach ($query as $row) {
echo $row;
};
Is it because of we shouldn't put '' into the query?
I mean:
$query = $this->dbConnection->query("SELECT * FROM ($questionTable) WHERE id = ($questionID)");
Related
This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
If you create a variable inside a if statement is it available outside the if statement?
(4 answers)
How to replace "if" statement with a ternary operator ( ? : )?
(16 answers)
If variable equals value php [duplicate]
(4 answers)
Closed 2 years ago.
There are a lot of examples on SO of using str_replace to modify a query that uses variables in MYSQL but I can't find one that solves my problem.
I have the following legacy query that I'm debugging written in PHP and MySQL.
Somewhat simplified it is:
$sql = "SELECT * from MOVIES WHERE cat = '$cat'";
In the event that cat has a certain value, say, "action" I want to check for "adventure";
Let's say you start with query:
$cat = "action";
$sql = "SELECT * FROM MOVIES WHERE cat='$cat'";
I'm trying to modify the query with:
$needle = "cat=".$cat;
$altcat = "adventure";
$altwhere = "cat=".altcat;
$sql = str_replace($needle,$altwhere,$sql); //NOTHING GETS REPLACED...NOT MATCHING Needle
How can I do this? I'm thinking the problem has something to do with use of spaces or apostrophes in the sql string but can't get it to work.
Thanks for any suggestions.
You want to replace "cat='".$cat."'" with "cat='adventure'", not "cat=".$cat with "cat=adventure".
(Though you are inconsistent in saying if there are spaces around the =.)
But you should not do this and should use a placeholder instead.
I would not try to do string substitution on the SQL query. Instead, just use query parameters.
$cat = 'action'; // suppose this is the input to your program
$sql = "SELECT * from MOVIES WHERE cat = ?";
if ($cat == 'action') {
$cat = 'adventure';
}
$stmt = $db->prepare($sql);
$stmt->execute( [ $cat ] );
This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
PDO: error handling
(2 answers)
Closed 3 years ago.
I get this error Invalid parameter number: parameter was not defined in C:\wamp\www\stage\core\addtUsers.php on line 20
if(!empty($_POST["add_record"])) {
require_once("connection.php");
$data = [
'username' =>$_POST["username"],
'password' =>$_POST["password"],
'role' =>$_POST["role"],
'photo' =>$_POST["photo"],
'nom-prenom' =>$_POST["nom-prenom"]
];
$sql = "INSERT INTO users(username,password,role,photo,nom-prenom) VALUES (:username,:password,:role,:photo,:nom-prenom)";
$statement = $pdo->prepare( $sql );
$result = $statement->execute($data);
if (!empty($result) ){
header('location:users.php');
}
}
You can't use - in the name of a placeholder. This will be understood as an arithmetic subtraction.
Consider this example:
$pdo->prepare('SELECT :num-1')->execute(['num'=>5]);
It looks for a param called :num or num and then it will subtract 1 from it.
A good IDE might even help you out by highlighting this:
Although with letters it would highlight it the same color as a column name, which could also be missed.
Try to use a different placeholder name e.g. :nom_prenom
This question already has answers here:
Single result from database using mysqli
(6 answers)
Closed 3 years ago.
I'm trying to echo the result of a mysqli_query however I keep getting the error 'Catchable fatal error: Object of class mysqli_result could not be converted to string' on line 'echo $result;'.
Is there any way I can convert it into a string so that it can be echoed? (P.S sorry if this is easy, I'm new to coding.)
My database is successfully connected and the SQL statements definitely work.
$sql= "SELECT ImageURL FROM `unnormalisedtable` WHERE Yeargroup = 9 ORDER BY RAND() LIMIT 1" ;
$result = mysqli_query($db, $sql);
echo $result;
The expected output is that the result of my SQLi query will be printed on screen, however the error is generated instead. Thanks for any help in advance.
Since you limit the selection to one entry use
$row = mysqli_fetch_array($result);
echo $row['ImageURL'];
If you select more than one entry loop over the result.
while($row = mysqli_fetch_array($result)) {
echo $row['ImageURL'];
}
This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 5 years ago.
I know this may sound like duplicate but it's not. I'm trying to do a simple SELECT query on a mysql table through PHP. This is the code I'm using:
<?php
$RTC_DB = mysqli_connect('localhost', 'root', 'telemedia-arte', 'rtc')
or die("Erro na ligação.. ".mysqli_connect_error());
mysqli_set_charset('utf8mb4_unicode_ci');
if (!$RTC_DB) {
echo "error";
} else {
$query = "SELECT * FROM 'prof' WHERE passo = 2";
$videos = mysqli_query($RTC_DB, $query);
if (mysqli_affected_rows($videos) > 0) {
echo "Woo!";
while ($video = mysqli_fetch_assoc($videos)) {
$video_courses[$video["id_aula"]] = $video["tituloaula"];
}
}
mysqli_close($RTC_DB);
}
?>
If the query returns at least one record, it will output "Woo!", but it's not. I tried the query in phpmyadmin and it works.
Can anybody please help me with this? I'm getting very worried with this.
Thanks in advance
Pedro
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
converting simple query to cake query?
Actually I have 1 query but I am unable to convert it into CakePHP query format.
$result = "select
* from esl_userresults, esl_lyrics
where
esl_userresults.esl_songID = esl_lyrics.id
and esl_lyrics.song_name like '%".$esl_keyword."%'" ;
When I convert this query into CakePHP it gives an error like:
esl_userresults.esl_songID unknown column.
You can execute any query using:
<?php
function some_controller_name()
{
$query_to_execute = 'SELECT * .....';
$results = $this->ModelName->query($query_to_execute);
$this->set('results', $results);
}
?>
Hope this helps