PHP stops after faile PDO query [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
I am running the below code and know that this is the suspect peice as I have placed an echo above and below it. The first runs fine but the second does not.
If I change the query to something like "show tables" it will run but I cannot see what if anything is wrong with this. I have checked reserved words and syntax. Am I missing something obvious!
try
{
$sth = $dbh->prepare('SELECT COUNT(*) AS val FROM users WHERE username=:user');
$sth->binvalue('user',$_POST['user']);
$sth->execute();
}
catch(PDOException $e)
{
echo $e->getMessage();
}

There is a type error:
instead of
$sth->binvalue('user',$_POST['user']);
Try
$sth->bindvalue('user',$_POST['user']);
^

$sth->binvalue('user',$_POST['user']);
should be
$sth->bindValue(':user',$_POST['user']);
Notice both the bindValue and the :user

Related

Query error after OR in MySQL and PHP [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I have problem with query to base, problem appears after OR:
Parse error: syntax error, unexpected 'or' (T_LOGICAL_OR) in
Here is the code:
$chat2=mysql_query('SELECT * from chat where
user1!="" OR user2!="" AND status="oczekuje" AND user1="'.$_SERVER['REMOTE_ADDR'].'"
OR user2="'.$_SERVER['REMOTE_ADDR'].'" '); or die();
$dodaj2=mysql_fetch_array($chat2);
$ilosc2=mysql_num_rows($chat2);
Here screen from sublime: http://scr.hu/47nh/haujn
Why "OR" is not on red color like AND?
Right now, or die(); is a separate statement because there is a ; right before it. You should change to
$chat2=mysql_query('SELECT * from chat where
user1!="" OR user2!="" AND status="oczekuje" AND user1="'.$_SERVER['REMOTE_ADDR'].'"
OR user2="'.$_SERVER['REMOTE_ADDR'].'" ') or die();
Furthermore, you want to consider:
use mysqli_* or PDO to prevent SQL injection attacks
add some parentheses to the query. How can you know that MySQL evaluates AND and OR in the same way as intended?
To elaborate point 2, AND takes precedence over OR, so it is evaluated as
user1!="" OR
(user2!="" AND status="oczekuje" AND user1="'.$_SERVER['REMOTE_ADDR'].'") OR
user2="'.$_SERVER['REMOTE_ADDR'].'"

PHP + MYSQL create table [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
SO, I want to create a pretty big mysql table that should save all of the squares of a chess board and their value, I'm using a PHP for() since it has 64 squares.
This always gives me 'not working'
<?php
mysql_connect('localhost','pierostesting','');
mysql_select_db('my_pierostesting');
$query="CREATE TABLE board (";
for($i=0;$i<63;$i++){
$query .= "i".$i." INT(1) NOT NULL,";
}
$query .="turno BOOL";
if (mysql_query($query)){
echo 'working';
}else echo 'not working'; ?>
I'm an idiot, didn't close the bracket.
try echoing out your complete SQL string, then use phpMyAdmin and see if the sql string actually works, or if it throws errors. You may have a sql syntax error.
If it does work, make sure your connection variables are correct (host, db name, username, password)
that should get you close to the answer.

Update record number unexpectedly fails [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
I am trying to get this to update a record and was wondering why I cant get this to work?
I can write a new record no problem but the update doesn't work; is it something simply I am missing?
else {
echo "ID NOT EMPTY-";
//---inside else statement shows this echo and the $id number printed on screen WHERE should have varible $ID but I changed it to debug
echo "$id";
$SQLstring3 = "UPDATE $Tablename SET(blank='1') WHERE(id='5')";
$QueryResult = #mysql_query($SQLstring3, $DBConnect);
}
I think the problem might be the way you are using SET and WHERE. Putting them the parentheses () immediately following them might make them seem like a MYSQL function, which it tries to execute. What you have is this:
"UPDATE $Tablename SET(blank='1') WHERE(id='5')";
Try removing the parentheses:
"UPDATE $Tablename SET blank='1' WHERE id='5'";

Php help find where is syntax error [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
foreach($db->fetch_array("SELECT id_categories FROM csn_categories_join_kartes where id_kartes===".$card['id']."") as $kat){
echo (kat['id_categories']);
}
table cols and values are all matched, something is wrong in this part of code
I tried adding $ before kat and using only one "=", sill doesnt work
NEW LINK
http://pastebin.com/RPK7vEaJ
this
where id_kartes===".$card['id']."
would be
where id_kartes=".$card['id']."
and missing $
echo $kat['id_categories'];
so full code :-
foreach($db->fetch_array("SELECT id_categories FROM csn_categories_join_kartes where id_kartes='".$card['id']."'") as $kat){
echo $kat['id_categories'];
}
best practice if you store your query result in a variable and loop over this variable.
foreach($db->fetch_array("SELECT id_categories FROM csn_categories_join_kartes where id_kartes=".$card['id']."") as $kat)

php PDO prepared statement doesn't work as expected [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
Possible duplicate, but I lost hours on this one
$stmt=$mysql->prepare(" insert into test_booked_tickets(book_id, ticket_id, num_people) values(:book_id,:ticket_id,:num)");
echo (int)$ticket->id."<br/>";
echo (int)$ticket->number_people."<br/>";
echo (int)$booking->id."<br/>";
$stmt->bindValue(":ticket_id", (int)$ticket->id);
$stmt->bindValue(":num", (int)$ticket->number_people);
$stmt->bindValue(":book_id", (int)$booking->id);
try{
$stmt->execute();
}
catch(PDOException $e){
echo "catched <br/>";
echo $e->errorInfo;
}
Other queries work perfectly. These are the returned values from echo
30
1
10
It doesn't get into catch, and it doesn't insert into table...
Thanks everyone for useful comments, I had a typo in database, column name.
Thanks A.O. for the most useful comment.
And thanks everyone else for other useful suggestions.

Categories