PHP SQL Syntax Check if theres a match [closed] - php

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am trying to query the db in php and get if there is a match / success but it's not resulting in success although it should so I've thinking there's a syntax error?
Here is the code:
if ($db_found) {
$SQL = "SELECT * FROM wp_users where user_login='.$user.'";
$result = mysql_query($SQL);
//Check if theres a match
if $result > 0 {
echo 'There was a match';
}
...
}

if (mysql_num_rows($result)) {
echo 'match';
}

if (mysql_num_rows($result)==1) {
echo 'There was a match';
}

Related

Why is this "if" condition ignored? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
This question does not appear to be about programming within the scope defined in the help center.
Closed 1 year ago.
Improve this question
The last if condition is ignored whereas one or both variables are equals to 0, can you explain me why ?
<?php
require('../controller/env.php');
$countexisting=0;
if(!isset($_GET['idkey'])){
header('location:../index.php');
exit();
}else{
$idkey=$_GET['idkey'];
$request=$db->prepare('SELECT * FROM testimonials WHERE idkey=?');
$request->execute(array($idkey));
while ($exist=$request->fetch()) {
$countexisting+=1;
$activelink = $exist['activelink'];
}
if($countexisting=0 || $activelink=0){
header('location:../index.php');
exit();
}
}
Thanks !
Missed the "==".
if($countexisting==0 || $activelink==0){
header('location:../index.php');
exit();
}
it's working fine now!

PhP database function [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I'm not sure how I can query a MYSQL database with PHP by incoming URL value. For example my API receives ?name=Radi and I want to check if there's user with name "Radi" in my database.
// How to define isNameExisting. Can I use PDO or something similar?
if (isNameExisting) {
echo"OK"
}
You can use php's PDO and MySQL's count() for that.
<?php
$pdo = new PDO('mysql:host=localhost;dbname=test;charset=utf8', 'localonly', 'localonly', array(
PDO::ATTR_EMULATE_PREPARES=>false,
PDO::MYSQL_ATTR_DIRECT_QUERY=>false,
PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION
));
$stmt = $pdo->prepare('
SELECT
Count(*) as c
FROM
tablename
WHERE
name=?
');
$stmt->execute( array($_REQUEST['NAME']) );
$row = $stmt->fetch();
if ( intval($row['c']) > 0 ) {
echo 'Ok';
}
else {
echo 'no such record';
}

MySQL result as variables [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
I have for example a var.php, that connect to a database, and read some data from there:
while($row = mysqli_fetch_assoc($result)) {
echo "$".$row["name"]. " = " . $row["value"]. ";<br>";
}
and the result looks like:
$VAR1 = 3.00000;
$VAR2 = 5.00000;
$VAR3 = 8.00000;
$VAR4 = 9.00000;
How can i embed this php (using echo or include or require) in another php, and will it work like variables? ( Sorry for my poor english :( )
You should take a look at variable variables (http://php.net/manual/en/language.variables.variable.php).
For your example:
while($row = mysqli_fetch_assoc($result))
{
$$row["name"] = $row["value"];
}

query for fetching only one integer value using php [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
hey i just want a query which will be printing only the result of count query . put in the else part
<?php
$con = mysql_connect('localhost', 'root', '') or die(mysql_error());
$db = mysql_select_db('quiz', $con) or die(mysql_error());
$q="count(*) from question where ans=uanswer";
$rq=mysql_query($q,$con);
if(!$rq)
{
echo " the sql query faiiled to work ";
}
else
{
}
?>
You would use the function mysql_fetch_row() to return a row from your database query as an array. You can then access the result for the count(*) as the first element in the returned array.
<?php
$con = mysql_connect('localhost', 'root', '') or die(mysql_error());
$db = mysql_select_db('quiz', $con) or die(mysql_error());
$q="count(*) from question where ans=uanswer";
$rq=mysql_query($q,$con);
if(!$rq)
{
echo " the sql query faiiled to work ";
}
else
{
$row = mysql_fetch_row($rq);
echo $row[0];
}
?>

Trouble executing transaction in PHP [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm having trouble getting a mysql transaction to work through PHP. I'm fairly new to PHP and very new to mysql.
If I take the var_dump of $query and try to run it through phpmyadmin it works fine.
$description =
mysql_real_escape_string($_REQUEST['description']);
$query = 'BEGIN;
INSERT INTO indiespark.tasks
(description, owner_user_id)
VALUES ("' . $description . '", '
. $user->user_id . ');
SET #task_id = LAST_INSERT_ID();
INSERT INTO indiespark.projecttasks
(task_id, project_id)
VALUES (#task_id, ' . $project->project_id . ');
COMMIT;';
$result = mysql_query($query);
var_dump($query);
var_dump($result);
if ($result) {
return viewproject();
} else {
throw new Exception('database error');
}
mysql_query doesn't support sending multiple queries in one call. Use separate calls.

Categories