this is my 1st php on the 1st place. if there is anything wrong with i did please help me with code. since i just started learning. i tried with so many tutorials but i was unable to undestand what im doing wrong.
<?php
include "db_config.php";
$query = mysql_query("SELECT * FROM places WHERE place_id ='".mysql_real_escape_string($_REQUEST[place_id])."'");
while($e=mysql_fetch_assoc($query))
$output[]=$e;
echo $row['name'];
mysql_close();
?>
Is this correct?
im not sure how it is working.
$stmt = $db->prepare("SELECT * FROM table WHERE $_REQUEST[place_id]");
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
You have a typo on this line:
while($e=mysql_fetch_assoc($q))
$q needs to be $query. I do not see a variable $q in your code. This is a common problem in php code as if you introduce a name like $q php is "nice" enough to create the variable for you and initialize it to null instead of sanely giving you an error.
You need to privode mysql_fetch_assoc() with the results of mysql_query, here $query.
<?php
include "db_config.php";
$query = mysql_query("SELECT * FROM places WHERE place_id='".mysql_real_escape_string($_POST[place_id])."'");
while($e=mysql_fetch_assoc($query))
$output[]=$e;
print(json_encode($output));
mysql_close();
?>
Moreover, you need to use some pdo or mysqli since mysql_ are deprecated: http://php.net/manual/en/function.mysql-query.php
Related
Apologies for opening what should be considered a very basic question. Please note I have coded using the mysql_query method 1000's of times thus, it is really difficult for me to make the switch but I'm finally doing it.
My 1st Problem
My 1st problem comes in simply returning a list of select statements
Example:I am trying to execute:
$sql="Select *From members";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
$name = $row['name'];
}
I have read the following SO questions, and tried to implement the answers without success,
PDO equivalent of mysql_fetch_array
translation mysql_fetch_array to PDO::FETCH_NUM
I am now working with a PHP book supposed to be "one of the best for 2015/16", the book says the following regarding the above problem
Again I have tried the recommendation from the book without success
<?php
$dsn ='mysql:localhost;dbname:myDB;';
$uname = 'root';
$pword = '';
$db = new PDO($dsn, $uname,$pword);
if($db){
echo'Success';
} else {
echo'error';
}
$sql="SELECT * from members";
$results = $db->query($sql);
foreach($results as $result) {
echo $result['name'];
}
The above code gives me the message, "success" thus, I am successfully connecting to my DB but the query returns the following error:
Invalid argument supplied for foreach()
I am finding it enormously frustrating that I have to go through such lengths just to return the results from a simple select statement and am seriously considering going back to the old way, where I would have completed my assignment by now, I am turning to the SO community for help in a last attempt to get it right. Any help greatly appreciated.
I am very ashamed of posting this, but I have found the problem. Possibly it might help someone in the future.
The code with querying the DB is fine.
The Problem is with the following in line 1:
$dsn ='mysql:localhost;dbname:myDB;';
It should read
$dsn ='mysql:host=localhost;dbname=myDB;';
The If statment will always return echo success even if you are not connected thus, use try/catch
lesson learned: Always inspect your code from line 1
<?php
include 'config.php';
$tid = $_GET['tid'];
$sql="SELECT FROM topics where tid='$tid'";
$result=mysql_query($sql);
while($rows=mysql_fetch_array($result)){
?>
<div class="topics"><font size="4"><?php echo $rows['title']; ?></font></div><div class="tdm"><br/><center><img src="http://appricart.com/test/img/<?php echo $rows['photo']; ?>" height="100" width="100"/><br/>
<small><?php echo $rows['message']; ?></small></div>
<?php
}
include 'foot.php';
?>
Sometime this code works sometimes it does not please help me with this problem.
It shows this error
mysql_fetch_array() expects parameter 1 to be resource
You aren't doing any error checks to prevent this. This error is caused when your MySQL query fails to execute. You have a mysql_query() statement, and the results are stored into a variable. Later, you're using that variable in mysql_fetch_array() statement.
If mysql_query fails, it returns FALSE which is a boolean value. mysql_fetch_array expects it to be a resource instead.
To solve this issue and understand why your query is failing, use the mysql_error() function.
$result = mysql_query($sql);
if (!$result) {
die('Query failed because: ' . mysql_error());
}
else {
//proceed
}
In this case, you aren't selecting anything from your database, as Sean mentioned above.
Try the following:
$sql = "SELECT * FROM topics where tid='$tid'";
Or
$sql = "SELECT topics FROM topics where tid='" . $tid . "'";
Also, please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO, or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.
<?php
include 'config.php';
$tid = $_GET['tid'];
$sql="SELECT FROM topics where tid='$tid'"; // here is the error
$result=mysql_query($sql);
You are not selecting anything from the table topic. hence its showing the error..
$sql="SELECT * FROM topics where tid='$tid'"; // this would be better
for more info visit mysql_fetch_array
I ran this code and I got a Resource id #3 error where it should have showed the full movies table.
mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("treehouse_movie_db") or die(mysql_error());
$data = mysql_query("SELECT * FROM movies")
or die(mysql_error());
echo $data;
This is not an error Your query is getting executed and you are getting appropriate resource from mysql_query() as it should be returned.
To get the response you have to use mysql_fetch_array() or mysql_fetch_assoc()
mysql_connect("localhost", "root", "password") or die(mysql_error());
mysql_select_db("treehouse_movie_db") or die(mysql_error());
$data = mysql_query("SELECT * FROM movies")
or die(mysql_error());
while($row = mysql_fetch_assoc($data))
{
print_r($row);
}
SUGGESTION: mysql_* are no longer maintained .Try switching to mysqli_* or PDO
You're not getting an error, the MySQL API is just doing what you're asking it to: echoing the contents of $data, which is a MySQL query resource at this point. Extend the code to actually retrieve the results:
while($row = mysql_fetch_object($data))
var_dump($row);
And you'll see the output.
Note that the mysql_* API is deprecated since PHP 5.5 by the way.
Resourse id #3 means that the $data variable was used to open a resourse, it is not an error.
If you were to open another resourse, like a file, using:
$var=fopen('myfile','a+');
echo $var;
You would get Resourse id $4 as a result.
So to get the output you desire, you need to use a loop.
It is described in here.
I have a project (exisiting) and I am ordered to continue it
but there's something strange
in my connection
<?php
include "adodb5/adodb.inc.php";
$config['mysqlhost'] = 'localhost';
$config['mysqluser'] = 'xxx';
$config['mysqlpass'] = 'yyy';
$config['mysqldb'] = 'zzz';
$DB = ADONewConnection('mysql');
$DB->Connect($config['mysqlhost'],$config['mysqluser'],$config['mysqlpass'],$config['mysqldb'],true);
?>
and if I try to call query (same queries as below) from this page, it works (and when I echo, it shows the value)
So I go to other page
<?
include ("inc/con.php");
?>
<?php
$sql = ("SELECT * FROM table");
$query = mysql_query($sql)or die($myQuery."<br/><br/>".mysql_error());
$result = mysql_fetch_array($query);
echo $result ['table id'];
?>
and the result is
Notice: Undefined variable: myQuery in C:\xampp\htdocs\d88\www\mypage.php on line 9
No database selected
is there anything wrong with it?
since i try on con page, it works and when i include it to other page, it not working
You are not defining any $myQuery either in inc/con.php nor in the same file itself. Also you are not selecting any database with mysql_select_db:
mysql_select_db($config['mysqldb']);
You are suggest, also, not to use mysql_* functions as they are going to be deleted and are yet deprecated (and you can use PDO or mysqli).
Notice: I think $sql = ("SELECT * FROM table") gets evaluated as $sql = true.
You can not connect with ADODB connection and establish a query with mysql_query.
the syntax is something like this mysql_query ($query ,$con). $con is optional but if you do not specify it, the last link opened by mysql_connect() is assumed; but you have not any mysql_connect() statement before
because of my version of php, i must use <?php ?> instead of <? ?>
thanks for helping
I have created two functions one for connecting to MySQL database and one for running a specific query.
I enter the database name as parameter for first function to connect to the database, this works fine, but my problem is with the second one.
2nd function returns the $result from running a query, but when I use mysql_fetch_array with the $result, it gives one output even if it supposed to give more than one.
As I am no php expert so i can't find the solution. Please help me.
Here is the code:
File Function.php
<?php
function myconnect($data)
{
$db_host='localhost';
$db_user='root';
$db_pwd='';
$data=$data;
$dbc = mysqli_connect($db_host, $db_user,$db_pwd,$data) or die (mysql_error());
return $dbc;
}
function runquery($db,$table,$tcol,$tid)//(databse,table,column_name,identifier)
{
$dbc=myconnect($db);
$query="SELECT *FROM ".$table." WHERE ".$tcol."=".$tid." ORDER BY first_name ASC";
$result = mysqli_query($dbc, $query);
return $result;
}
?>
File test.php
<?php
require_once('testfunc.php');
$result= runquery('user','user_basic','user_type','1');
//runquery('database','table','col','id')/
while($row=mysqli_fetch_array($result))
{
echo '<strong>First Name:</strong>' . $row['first_name'] . '<br/>';
}
?>
If I am doing all wrong then suggest me a better way :-)
A quick glance shows that in your function runquery
SELECT *FROM
should be
SELECT * FROM
note the space after the *
EDIT :
I also notice you are using *mysqli_fetch_array* and this is not a valid mysqli method. You are right in using the mysqli extension over mysql but you should look more into statement fetch to solve this issue. The link I provided give a procedural example that should work for what you need.
function myconnect($db)
{
/*Removed redundant - single use variables*/
/*DB name was passed to the client_flags parameter of mysql_connect instead of mysql_select_db*/
$dbc = mysql_connect("localhost", "root","") or die (mysql_error());
/*Inserted Line*/
mysql_select_db($data);
return $dbc;
}
Currently you're not selecting a database - equivalent of USE DATABASE db_name.
Couple of syntax changes and function definition
function runquery($db,$table,$tcol,$tid)//(databse,table,column_name,identifier)
{
$dbc=myconnect($db);
/*Query and link identifier were in the wrong order*/
return mysql_query("SELECT * FROM ".$table." WHERE ".$tcol."=".$tid." ORDER BY first_name ASC", $doc);
}
Finally a couple of syntax changes, function calls
require_once('testfunc.php');
$result= runquery('user','user_basic','user_type','1');
/*fetch associateive array of result during iteration*/
while($row=mysql_fetch_assoc($result))
{
echo '<strong>First Name:</strong>' . $row['first_name'] . '<br/>';
}