I have a script that reads my database table fields. Its not reading the first column which is the id.It reads the other fields and adds them into the array. I have added in the for loop a -1 to get every field but to no success.
$host=rtrim($_POST['host']);
$user=rtrim($_POST['user']);
$pass=rtrim($_POST['pass']);
$dbselect=rtrim($_POST['dbselect']);
$table=rtrim($_POST['table']);
$classname=rtrim($_POST['classname']);
$key_values = array();
$link = mysql_connect($host,$user,$pass);
$db_select = mysql_select_db($dbselect);
$query = mysql_query('SHOW COLUMNS FROM '.$table.'');
if (!$link) {
die('Could not connect to MySQL server: ' . mysql_error());
}
$dbname = $dbselect;
$db_selected = mysql_select_db($dbname, $link);
if (!$db_selected) {
die("Could not set $dbname: " . mysql_error());
}
$res = mysql_query('select * from '.$table.'', $link);
$num_fields = mysql_num_fields($res);
for($i=0;$i<$num_fields;$i++){
$key_values[]=mysql_field_name($res,$i);
}
echo "<pre>";
print_r($key_values);
echo "</pre>";
There is no more support for mysql_* functions, they are officially deprecated, no longer maintained and will be removed in the future. You should update your code with PDO or MySQLi to ensure the functionality of your project in the future.
<?php
$host=rtrim($_POST['host']);
$user=rtrim($_POST['user']);
$pass=rtrim($_POST['pass']);
$dbselect=rtrim($_POST['dbselect']);
$table=rtrim($_POST['table']);
$classname=rtrim($_POST['classname']);
$db = new mysqli($host,$user,$pass,$dbselect);
if($db->connect_error)
die('Connect Error (' . mysqli_connect_errno() . ') '. mysqli_connect_error());
// NOTE real_escape_string may not work for tables untested
$result = $db->query("SELECT * FROM " . $db->real_escape_string($table));
if (!$result)
die "Error: " . $db->error;
while ($row = $result->fetch_object())
{
echo $row->id;
}
$result->close();
$db->close();
I don't see why it might be doing that, but this should be more reliable:
$query = mysql_query('select * from `%s`', mysql_real_escape_string($table), $link);
while ($result = mysql_fetch_array($query)) {
print_r(array_keys($result));
}
Try to use php native function mysql_fetch_array (also you need view this quastion before)
After try this code ($res === 'resources'):
$res = mysql_query('select * from '.$table.'', $link);
while ($row = mysql_fetch_array($res, MYSQL_ASSOC)) {
$key_values[] = array_keys($row);
}
echo "<pre>";
print_r($key_values);
echo "</pre>";
Related
I do not understand what is going wrong with code. The result is get is "connected successfully success Query failed". I tried few combinations and I get the same result. Please help me in solving this. Thanks in advance.
<?php
$link = mysql_connect('localhost', 'root1', '')
or die('Could not connect: ' . mysql_error());
if ($link) {
echo 'connected successfully';
}
$l = mysql_select_db('vtflix', $link) or die ('Could not select the database');
if ($l) {
echo ' success';
}
/*$varCNAME = 'John';
$varCONTENT = '4';
$varVID = '1';*/
$sql = "INSERT INTO mpaa(C_Name, ContentRating, V_ID) VALUES ('Jon', 4, 3)";
mysql_query($sql, $link) or die("Query failed");
$que = "SELECT * FROM mpaa";
$query = mysql_query($que, $link);
if (!$query) {
echo 'query failed';
}
while ($sqlrow = mysql_fetch_array($query, MYSQL_ASSOC)) {
$row = $sqlrow['C_Name'];
$nrow = $sqlrow['Content Rating'];
$mrow = $sqlrow['V_ID'];
echo "<br>" . $row . " " . $nrow . " " . $mrow . "<br>";
}
mysql_close($link);
?>
1.Don't use mysql_* library (deprecated from php5 onward + removed from php7) .Use mysqli_* OR PDO.
2.An example of mysqli_*(with your code)is given below:-
<?php
error_reporting(E_ALL); // check all type of error
ini_set('display_errors',1); // display those errors
$link = mysqli_connect('localhost', 'root1', '','vtflix');
if($link){
echo 'connected successfully';
$sql= "INSERT INTO mpaa(C_Name,ContentRating,V_ID) VALUES ('Jon', 4, 3)";
if(mysqli_query($link,$sql)){
$query = "SELECT * FROM mpaa";
$res = mysqli_query($link,$query);
if($res){
while($sqlrow=mysqli_fetch_assoc($query))
{
$row= $sqlrow['C_Name'];
$nrow= $sqlrow['Content Rating'];
$mrow= $sqlrow['V_ID'];
echo "<br>".$row." ".$nrow." ".$mrow."<br>";
}
mysqli_close($link);
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Could not connect: ' . mysqli_connect_error());
}
?>
Note:- To check php version (either on localhost or on live server) create a file with name phpInfo.php, and just write one line code in that file:-
<?php
phpinfo();
?>
Now run this file and you will get the current php version.
Like this:- https://eval.in/684551
Here it seems that you are using deprecated API of mysql_* .
1) Check your PHP version
<?php phpinfo();exit;//check version ?>
2) avoid the usage of mysql use mysqli or PDO
3) change your db connection string with this :
new Mysqlidb($hostname, $username, $pwd, $dbname);
example with you code
<?php
$link = mysqli_connect('localhost', 'root1', '','vtflix');
if($link){
echo 'connected successfully';
$sql= "INSERT INTO mpaa(C_Name,ContentRating,V_ID) VALUES ('Jon', 4, 3)";
if(mysqli_query($link,$sql)){
$query = "SELECT * FROM mpaa";
$res = mysqli_query($link,$query);
if($res){
while($sqlrow=mysqli_fetch_assoc($query))
{
$row= $sqlrow['C_Name'];
$nrow= $sqlrow['Content Rating'];
$mrow= $sqlrow['V_ID'];
echo "<br>".$row." ".$nrow." ".$mrow."<br>";
}
mysqli_close($link);
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Query error: ' . mysqli_error($link));
}
}else{
echo die('Could not connect: ' . mysqli_connect_error());
}
?>
I have a php code for some MYSQL interogations,
Code is:
$DBTYPE = 'mysql';
$DBHOST = 'localhost';
$DBUSER = 'tuser';
$DBPASSWORD = 'password';
$DBNAME = 'dbname';
$link = mysql_connect($DBHOST, $DBUSER, $DBPASSWORD);
mysql_select_db($DBNAME);
if (!$link) {
die('Could not connect: ' . mysql_error());
}
//IMG**0**
$hotelc = $hotelCodes[**0**];
$result = mysql_query("SELECT ImageURL FROM Flat_table where HotelCode= '$hotelc'", $link);
if(!$result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
$ImageURL**0** = $row["ImageURL"];
}
//IMG**1**
$hotelc = $hotelCodes[**1**];
$result = mysql_query("SELECT ImageURL FROM Flat_table where HotelCode= '$hotelc'", $link);
if(!$result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
$ImageURL**1** = $row["ImageURL"];
}
..........................
//IMG**x**
$hotelc = $hotelCodes[**x**];
$result = mysql_query("SELECT ImageURL FROM Flat_table where HotelCode= '$hotelc'", $link);
if(!$result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
$ImageURL**x** = $row["ImageURL"];
}
The repeating value on each code line is bolded.
How can i create a Mysql parameterized queries in php.n to avoid write all the lines .I need to extract ~100 $ImageURL from the Flat_table where the $hotelc is found.
For example you have to repeat it $N times:
for($i=0; $i<$N; $i++)
{
$hotelc = $hotelCodes[ $i ];
$result = mysql_query("SELECT ImageURL FROM Flat_table where HotelCode= '$hotelc'", $link);
if(!$result) {
die("Database query failed: ".mysql_error());
}
while ($row = mysql_fetch_array($result)) {
${'ImageURL'+$i} = $row["ImageURL"];
}
}
To loop, use for:
for($n=0; $n<100; $n++){
$hotelc = $hotelCodes[$n];
$result = mysql_query("SELECT ImageURL FROM Flat_table where HotelCode= '$hotelc'", $link);
if(!$result) {
die("Database query failed: " . mysql_error());
}
while ($row = mysql_fetch_array($result)) {
$ImageURL[$n] = $row["ImageURL"];
}
}
But the inner functions inside the loop is inefficient because mysql query will be executed 100 times. You can query all the ImageURL by using IN() syntax in mysql:
//Wrap all hotelCodes into one string for query, like ["a","b"] to "'a','b'"
$len = count($hotelCodes);
foreach($hotelCodes as $key=>$code){
$hotelCodes[$key] = "'".$code."'";
}
$codesStr = implode(",", $hotelCodes);
$result = mysql_query("SELECT ImageURL FROM Flat_table where HotelCode IN (".$codeStr.")", $link);
//Other things...
When writing a function, you look for the commonality. Furthermore, you want to minimize the database interaction. I am, for the sake of deprecation, going to assume $link uses mysqli_connect()
$ImageURL = array();
$list = implode('", "', $hotelCodes);
$result = mysqli_query($link, 'SELECT ImageURL FROM Flat_table where HotelCode IN "' . $list . '"');
while($row = mysql_fetch_assoc($result)) {
$ImageURL[] = $row["ImageURL"];
}
This runs only one query and then loops through the results, generating an associative array. So echo $ImageURL[0]; will output your first URL.
This is the structure in the database:
items |itemLink
----------------------
Kill Bill|Kill Bill link
Preman |Preman link
This is the code:
$db = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
$items = 'SELECT items FROM menus';
$itemLink = 'SELECT itemLink FROM menus';
$itemQuery = $db->query($items);
$linkQuery = $db->query($itemLink);
$fetchItem = $itemQuery->fetchAll(PDO::FETCH_ASSOC);
$fetchLink = $linkQuery->fetchAll(PDO::FETCH_ASSOC);
$merged = array_merge($fetchItem,$fetchLink);
foreach($merged as $entry) {
foreach( $entry as $key => $value ) {
}
}
From the above code, how do I output only the items' datas?
Using the example above you could then do something like this to answer you question
$result = mysql_query('Select * from names');
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
echo $row["FirstName"] . " " . $row["LastName"] . "<br>";
}
mysql_close($conn);
?>
I would use something like this, not two arrays for something that could be one query. I have shown three methods, using var_dump or print_r will show how each works.
$conn = mysql_connect($hostname, $username, $password);
if (!$conn) {
die('Could not connect to MySQL: ' . mysqli_connect_error());
}
$db_selected = mysql_select_db('sample', $conn);
if (!$db_selected) {
die("Can\t use db : ' . mysql_error()");
}
$result = mysql_query('Select * from names');
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
print_r($row);
}
$result = mysql_query('Select * from names');
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
print_r($row);
}
$result = mysql_query('Select * from names ');
while ($row = mysql_fetch_array($result, MYSQL_BOTH)) {
print_r($row);
}
mysql_close($conn);
$sqlQuery = "SELECT * FROM allowedUsers WHERE UserID = '" . $kUserID . "'";
$result=mysql_query($sqlQuery, $db);
if(!result)
{
echo "Error running query <br>" . mysql_error();
exit;
}
while($row = mysql_fetch_array($result))
{
echo $row[2];
}
I run the SQLQuery in phpMyAdmin and I am getting a valid result (1 row)
the table (allowedUsers) has 6 fields
I can't get anything out of the DB.
Any help is appreciated.
if(!result) should be if(!$result)
According to PHP.net's documentation, you don't need to pass $db to mysql_query(). Take a look at the example code:
<?php
mysql_connect("localhost", "mysql_user", "mysql_password") or
die("Could not connect: " . mysql_error());
mysql_select_db("mydb");
$result = mysql_query("SELECT id, name FROM mytable");
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
printf("ID: %s Name: %s", $row[0], $row[1]);
}
mysql_free_result($result);
?>
It may be helpful to see your connection code, ensure you've selected a database, etc.
I am trying the following code:
<?php
$link = mysql_connect('localhost', 'root', 'geheim');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
$query = "SELECT * FROM Auctions";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
foreach($row as $field=>$value)
{
echo "$field: {$value} <br />";
}
}
mysql_close($link);
?>
And get this error:
Warning: mysql_fetch_array(): supplied argument is not a
valid MySQL result resource in
C:\Programme\EasyPHP 2.0b1\www\test.php on line 14
What am I missing?
You haven't selected a database - use mysql_select_db()
That would be something like:
<?php
$link = mysql_connect('localhost', 'root', 'geheim');
if (!$link) {
die('Could not connect: ' . mysql_error());
}
echo 'Connected successfully';
$db_selected = mysql_select_db('foo', $link);
if (!$db_selected) {
die ('Error selecting database: '. mysql_error());
}
echo 'Using database successfully';
$query = "SELECT * FROM Auctions";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
foreach($row as $field=>$value) {
echo "$field: {$value} <br />";
}
}
mysql_close($link);
?>
Your MySQL query possibly does not match any rows in the database.
Check the return value of mysql_query(), which returns "resource" on success and "false" on failure.
$query = "SELECT * FROM Auctions";
$result = mysql_query($query);
if ($result !== false) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
foreach ($row as $field=>$value) {
echo $field . ':' . $value
}
}
} else {
// query returned 0 rows
}
As others also suggested, you can use mysql_error() to look if the query returns any mySQL errors
$query = "SELECT * FROM Auctions";
$result = mysql_query($query) or die(mysql_error());
so you'll see the error
Are you getting anything returned? If no results are found, mysql_query returns FALSE.
Check that before running fetch_array.