Hi I have had a long search for this but have found no fix.
My code is as follows:
$link = mysqli_connect("localhost",".........","...........",".........") or die("Error " . mysqli_error($link));
$ctime = time();
$check = "SELECT * FROM thread WHERE forumid='48' AND visible='1' ORDER BY lastpost DESC LIMIT 1" or die("Error in the consult.." . mysqli_error($link));
//execute the query.
$rc = mysqli_query($link, $check);
while($rows = $rc->fetch_assoc()){
$pid = $rows['firstpostid'];
$query = "SELECT * FROM dropouts WHERE date <= $ctime" or die("Error in the consult.." . mysqli_error($link));
//execute the query.
$result = mysqli_query($link, $query);
$row_cnt = $result->num_rows;
while($row = $result->fetch_array())
{
$date = $row['date'];
$user = $row['username'];
$sql = "DELETE FROM dropouts WHERE date = $date" or die("Error in the consult.." . mysqli_error($link));
//execute the query.
$done = mysqli_query($link, $sql);
//////////////////////////////////////////////////////////////////////////\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
$check1 = "SELECT * FROM post WHERE postid='$pid'" or die("Error in the consult.." . mysqli_error($link));
//execute the query.
$rc1 = mysqli_query($link, $check1);
while($row1 = $rc1->fetch_array())
{
$text = $row1['pagetext'];
echo str_replace($user, "", $text);
}
}
}
The problem is that when I run it like that I get no out put.
If I run it so that the while loops are not in eachother I get output but the script only does it for one row post/row.
Does anyone know how to fix this?
I read that it should work but this just isn't working...
Thanks
Have you tried echoing it. It can be that the query returns 0 or empty!
As a beginner in php, you may stumble into these kinds of issues. The best way to understand the problem would be to ECHO & EXIT. Check each step by doing this and you can better understand the problem.
In the problem above, instead of writing the entire code, first of all try checking the small portion of codes and remember:
The best compiler/interpreter lies between you two ears!!
Related
i want fetch all users data but am getting only one user details, please help me to solve
$conn = mysqli_connect("localhost", "root", "", "bitmining");
$sql6="SELECT username FROM users";
if($result = mysqli_query($conn, $sql6)){
while ($row=mysqli_fetch_array($result)){
//Hashrate Data Fetch
$investedusername = $row['username'];
$sql3="SELECT sum(hashrate_amount) as total FROM buyhashrate WHERE invested_username='$investedusername'";
$result = mysqli_query($conn, $sql3);
$row = mysqli_fetch_assoc($result);
//Total Value of Hashrate
echo $row['total'] . " GH/s";
echo "<br />";
}
$result->close();
}
Your re-using the $result field, change your second reference to something like ...
$result1 = mysqli_query($conn, $sql3);
$row = mysqli_fetch_assoc($result1);
This will stop it reseting the value your using for your main loop in
while ($row=mysqli_fetch_array($result)){
You use the $result variable for both mysqli_query
$result = mysqli_query($conn, $sql3);
While other answers are right about your mistake, I want to give you a better solution.
Try to use a join like this:
$conn = mysqli_connect("localhost", "root", "", "bitmining");
$sql="SELECT SUM(hashrate_amount) AS total FROM users AS t1 LEFT JOIN buyhashrate AS t2 ON (t1.username=t2.invested_username) GROUP BY t1.username";
if($result = mysqli_query($conn, $sql)){
while ($row=mysqli_fetch_array($result)){
//Total Value of Hashrate
echo $row['total'] . " GH/s";
echo "<br />";
}
$result->close();
}
This way you do just one query from database. But using your method you have n+1 queries which n is the number of users. So for one hundred users there is 101 queries.
I have a simple mysql table with 'ratee' being the user and rating being the rating given to that user. I want to display how many times that user has been rated and what the average of those ratings are. The former works as shown in the code below but the latter does not. Where am i going wrong please? I am using PHP.
//working
$sql = "SELECT * FROM ratings WHERE ratee='" . $user1 . "'";
$result = mysqli_query($conn, $sql);
$ratingsqty = mysqli_num_rows($result);
echo $ratingsqty;
//not working 1
$sql = "SELECT * FROM ratings WHERE ratee='" . $user1 . "'";
$result = mysqli_query($conn, $sql);
$rating = mysqli_avg($result);
echo $rating;
//not working 2
$sql = "SELECT avg(rating) FROM ratings WHERE ratee='" . $user1 . "'";
$rating = mysqli_query($conn, $sql);
echo $rating;
Your problem is that you do not even bother to go through a basic mysqli tutorial or read mysqli documentation.
Not working 1: There is no such function as mysqli_avg(). Period.
Not working 2: mysqli_query() returns a mysqli_result object, not the average. You can use one of the fetch_*() methods of mysqli_result object to get the actual average calculated by MySQL:
See the code below:
$sql = "SELECT avg(rating) FROM ratings WHERE ratee='" . $user1 . "'"; //in real life code pls take some measures to prevent sql injection attacks
$result = mysqli_query($conn, $sql); //in real life code pls check for execution errors
$rating = $result->fetch_array(MYSQLI_NUM)
echo $rating[0];
mysqli_query() returns a Mysql results object. As such, you can't just echo it. You'll still need mysqli_fetch_row() or similar.
Try this
$sql = "SELECT * FROM ratings WHERE ratee='" . $user1 . "'";
$result = mysqli_query($conn, $sql);
$rating = mysqli_fetch_array($result,MYSQLI_ASSOC);
now $rating is an array by this you can print data of your table
using: echo $rating['ratee']; or echo $rating['rating'];
For average try this:
$sql = "SELECT avg(rating) as av FROM ratings WHERE ratee='" . $user1 . "'";
$result = mysqli_query($conn, $sql);
$rating = mysqli_fetch_array($result,MYSQLI_ASSOC);
echo $rating['av'];
Perhaps too late for you but maybe others will benefit:
Get one line result from MySQL:
$val = fetch_one_record("SELECT some_field FROM some_table LIMIT 1);
function fetch_one_record($sql) {
$conn = db_connect();
$result = array_shift($conn->query($sql)->fetch_assoc());
$conn->close();
return $result;
}
the first query is fine but the second one wont work it just dies.
I plug the $city variable into the second one and echo it back and it shows the correct
value but its the the actual:
$row = mysqli_query($dbc, $query)
or die('Error while querying the Database');
that fails... please help!
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME)
or WriteMessage('Error', 'Could not connect to the Database...');
//get user city...
$userID = $_SESSION['userID'];
$queryUserCity = "SELECT * from user where userID = $userID";
$GetResult = mysqli_query($dbc, $queryUserCity)
or die('Error while querying the Database');
$getRow = mysqli_fetch_array($GetResult);
$city = $getRow['city'];
$state = $getRow['state'];
$username = $getRow['username'];
echo 'username='.$username.' ';
echo 'city='.$city;
echo 'state = ' .$state;
$query = "SELECT * FROM adds where city = $city ORDER BY addDate ASC";
//fails right here...
/*-->*/ $row = mysqli_query($dbc, $query)
or die('Error while querying the Database');
echo $query;
exit();
while($row = mysqli_fetch_array($data))
{
You are trying to find a string without single quote. You use integer without single quote but in case of string you have to use single quote with your string.
change
$query = "SELECT * FROM adds where city = $city ORDER BY addDate ASC";
to
$query = "SELECT * FROM adds where city = '$city' ORDER BY addDate ASC";
and to find out exact error try to use the below code.
if (!mysqli_query($dbc, $query)){
echo("Error description: " . mysqli_error($dbc));
}
If the city is a textual type (and it probably is), you'll need:
... where city = '$city' ...
But, in fact, you shouldn't really be doing it that way anyway, since it opens you up to the possibility of SQL injection attacks if someone can enter arbitrary text for their city.
You should start looking into parameterised queries since they can protect you from such attacks. See Exploits of a Mom and the invaluable explain-xkcd entry.
Link to Mysql Table
I'm trying to have a basic website display info from a table to use as content. I run this
$result = mysqli_query($conn,"SELECT cont_id, cont_text FROM content") or die("Error in the consult.." . mysqli_error($conn));
$row = mysqli_fetch_array($result);
Each page_location is a page on the website. I would like to do something like
<?php echo $row['cont_text'] WHERE cont_id = 15; ?>
I know that's not right but then down the page I would echo cont_text from a new cont_id. How would I go about this?
Do I need to reorganize my table? Or multiple mysqli_query's?
You're over-thinking it. Just run one query using cont_id in the where clause of the query:
$result = mysqli_query($conn,"SELECT cont_text FROM content WHERE cont_id = 15") or die("Error in the consult.." . mysqli_error($conn));
$row = mysqli_fetch_array($result);
echo $row['cont_text'];
edit
Based on your comments try this:
$result = mysqli_query($conn,"SELECT cont_id , cont_text FROM content") or die("Error in the consult.." . mysqli_error($conn));
$content = array();
while($row = mysqli_fetch_array($result)) {
$content[$row['cont_id']] = $row['cont_text'];
}
echo $content[15];
I'm stumped. I run the script, no errors are shown, but neither is data inserted into the DB.
Here's my code:
include("db_con.php");
$conn = mysql_connect($db_host, $db_user, $db_pass) or die(mysql_error());
mysql_select_db($db_name) or die(mysql_error());
for ($i = 1; $i <= 5; $i++) {
$urls[$i] = mysql_real_escape_string($_POST['url_' . $i]);
$names[$i] = !empty($_POST['name_' . $i]) ? mysql_real_escape_string($_POST['name_' . $i]) : mysql_real_escape_string($_POST['url_' . $i]);
}
$query = "SELECT * FROM multi_url";
$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);
$page_id = $num_rows++;
$query2 = "INSERT INTO multi_url (page_id, url_1, url_2, url_3, url_4, url_5, name_1, name_2, name_3, name_4, name_5) values ($page_id, $urls[1], $urls[2], $urls[3], $urls[4], $urls[5], $names[1], $names[2], $names[3], $names[4], $names[5])";
mysql_close($conn);
Any comments on the matter would be greatly appreciated. If there's a problem, I would've thought I'd get an error from die(mysql_error()).
You do not send any INSERT query to the database. You just put the SQL in a variable called $query2.
mysql_query($query2); //sends the query to the database
That query will fail though, since none of its string columns are enclosed in quotes.