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 5 years ago.
Improve this question
Here is my function .Earlier it was working in a very better way but now a day it is not working well .
function table_rows()
{
$sql="SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='check_db' AND `TABLE_NAME`='tbl_details'";
$result= mysql_query($sql);
return $result;
}
The function results..Something like this .I want only field names.
COLUMN_NAME
tbl_structure.php?change_column=1&field=id&token=8a5756ad27bfde74d341bfed684767e5
tbl_structure.php?change_column=1&field=fnme&token=8a5756ad27bfde74d341bfed684767e5
tbl_structure.php?change_column=1&field=lnme&token=8a5756ad27bfde74d341bfed684767e5
tbl_structure.php?change_column=1&field=age&token=8a5756ad27bfde74d341bfed684767e5
tbl_structure.php?change_column=1&field=dob&token=8a5756ad27bfde74d341bfed684767e5
tbl_structure.php?change_column=1&field=mail&token=8a5756ad27bfde74d341bfed684767e5
tbl_structure.php?change_column=1&field=ads&token=8a5756ad27bfde74d341bfed684767e5
first, mysql_ is deprecated. Use mysqli_ or PDO
second, you are only executing the sql query, but not fetching any data. Try the code below. Note that $con is the variable to connect to db. You should change it to your own
(Edited)
https://www.w3schools.com/php/func_mysqli_query.asp
https://www.w3schools.com/php/func_mysqli_fetch_array.asp
function db () {
static $con;
if ($con===NULL){
$con = mysqli_connect ("localhost", "root", "", "database");
}
return $con;
}
function table_rows()
{
$con = db();
$sql="SELECT `COLUMN_NAME` FROM `INFORMATION_SCHEMA`.`COLUMNS` WHERE `TABLE_SCHEMA`='check_db' AND `TABLE_NAME`='tbl_details'";
$query= mysqli_query($con,$sql);
$result= mysqli_fetch_array($query,MYSQLI_ASSOC);
return $result;
}
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
So basically I got this code right here:
<?php
include_once 'dbconfig2.php';
$con = new DB_con();
$table = "users";
if(isset($_GET['profile_id']))
{
$sql=mysql_query("SELECT * FROM users WHERE user_id=".$_GET['profile_id']);
$result=mysql_fetch_array($sql);
}
?>
I am clueless as to how I would make it so if the user_id is not existent in the records, they cannot view their profile but it leads them to another messsage or piece of code.
If the user_id doesn't exist, there won't be any rows in the result. When you try to read a row with mysql_fetch_array(), it returns FALSE. So you can simply test $result:
if (!$result) {
die("Invalid profile ID");
}
Try to use prepared statements using mysqli, in order to avoid sql injection.
By way of example:
$mysqli = new mysqli("localhost", "root", "root", "test");
if ($mysqli->connect_errno) {
echo "connect_error". $mysqli->connect_error;
}
$id = $_GET['profile_id'];
$result = $mysqli->prepare('SELECT name FROM users WHERE user_id = ?');
$result->bind_param("i", $id);
$result->execute();
$result->bind_result($col1);
$result->fetch();
$is_valid_profile = (!$col1) ? 'Invalid profile' : 'Valid profile';
echo $is_valid_profile;
$result->close();
http://php.net/manual/en/mysqli.prepare.php
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';
}
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 want to execute these lines When I click on the button:
$cijfer = mt_rand(1, 25);
$query = "select word from dba_tblwords where wordId = '$cijfer'";
It will pick another word from the database using that query.
The button in the HTML page:
echo "<A HREF=$self?n=$n>Play again.</A>\n\n";
How can I do this?
To actually run the query you can use MySQLi. Here is a good beginners guide.
//Connect to database.
$db = new mysqli('localhost', 'user', 'pass', 'database');
//The code you provided.
$cijfer = mt_rand(1, 25);
$query = "select word from dba_tblwords where wordId = '$cijfer'";
//Run the query.
$result = $db->query($sql);
//Get the first (and presumably only) row
$row = $result->fetch_assoc();
//Output the word.
echo $row['word'];
//Free up some memory.
$result->free();
To get the link to work, you need to have the name of the page. You can either hardcode it, or use $_SERVER['PHP_SELF']:
echo 'Play again!'
(I don't know what you use the $n for so I just left it in there.)
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
a.php
include 'function.php';
$avg = get_ecomm_avg_rating();
echo "Avg rating :- $avg";
function.php
$conn = mysqli_connect("myserver","abc","red", "live");
if (mysqli_connect_errno())
{
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
function get_ecomm_avg_rating()
{
$query = "SELECT AVG(ROUND((r.ratings_sum / r.ratings_qty),1)) AS total_rating
FROM abc AS c, xyz AS r
WHERE c.store
IN (994,094)
AND r.reviewid = c.id
AND c.published = '1'";
$avg_rating_result = mysqli_query($conn,$query);
$avg_rating_row = mysqli_fetch_array($avg_rating_result);
$avg_rating_count = $avg_rating_row[0];
$avg_rating_count_final = number_format($avg_rating_count, 1);
return $avg_rating_count_final;
}
Always return 0.0. What should I do? Any batter approach? I'm New to functions and PHP
There is a problem with a variable scope, var $conn doesn't exists in the function.
You need to add this var by parameter:
function get_ecomm_avg_rating($conn, $storeno) {
....
}
// and here when calling the function you pass there a DB connection
$avg = get_ecomm_avg_rating($conn);
// $conn has to be defined elsewhere, like $conn = mysqli_connect(...)
Then, you have there parameter $storeno which is unused.
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];
}
?>