Pass value in php - php

hi I have modules as a links in student page so after click on any one of these links i want to pass the id of the Specific module to the next page,so how can i pass the id value and this is my code, please help.
$sql = "SELECT * FROM MODULE,USER where user_access_level = module_level AND '$user_id' = user_login ";
$result = $db->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<li>'.$row["module_id"].' '.$row["module_name"].' '.$row["module_points"].'</li> ';
}
}

Here is your code with all of the quotes, concatenations, backticks and backward logic corrected:
$sql = "SELECT * FROM `MODULE`, `USER` WHERE `user_access_level` = 'module_level' AND `user_login` = '". $user_id."' ";
$result = $db->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo '<li>'.$row["module_id"].' '.$row['module_name'].' '.$row['module_points'].'</li> ';
}
}
I would bet that module_level is also supposed to be a variable. $G looks problematic, what sets the array $G? You should replace the $G with $row if I am reading this correctly.

...AND user_id = '".mysqli_real_escape_string ($user_login)."'";
I assume?
Anyway, DB_field = $param. And maybe mysql_, not mysqli_, but hope you check the user input before you pass it to the query...

Check the syntax error:
echo '<li>' . $row["module_id"] . ' ' . $row["module_name"] . ' ' . $row["module_points"] . '</li>';

Related

Multiple MySQL Queries (first one to get an id based on the user) second one to get information of another project in a database with the id

I am trying to accomplish the following:
Get the project id from a database where the username =...
-> Users can be signed into different projects and it is being tracked in a database
Next step is to get information of the project database to display it for the user
-> But since the user can be signed into different projects I want him to see them underneath each other in a not yet specific order (I a
This is basically what I was trying to get to work and it displays the right content but unfortunately stops at the first project. My assumption is that it stops at the first project id and doesn't continue
-> I checked to see what code of the code works and as soon as is cut the information gathering query I get all project_id's
Here is basically the code that I am working with right now:
$sql = "SELECT project_id FROM project_members_db WHERE user_ID = '$user_id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
//Album Informationen ziehen herausfinden
$sql = "SELECT project_name, sticker_count, manufacturer, sport, creation_datetime FROM project_db WHERE project_id = '$row[project_id]'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
//OUTPUT
echo "
$row['project_name'];
$row['count'];
$row['sport'];
$row['manufacturer'];
$row['creation_datetime'];
";
}
}
}
}
It looks like you ought to be able to do this with a single query - and as you are taking user supplied input you ought to consider using a prepared statement
$sql='select `project_name`, `sticker_count`, `manufacturer`, `sport`, `creation_datetime`
from `project_db` where `project_id` = ( select `project_id` from `project_members` where `user_id` = ? )';
$stmt=$conn->prepare( $sql );
$stmt->bind_param('s',$user_id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($project_name,$sticker_count,$manufacturer,$sport,$creation_datetime);
while( $stmt->fetch() ){
echo $project_name,$sticker_count,$manufacturer,$sport,$creation_datetime;
}
You overwrite the first result with the second result. Change to:
$sql = "SELECT project_id FROM project_members_db WHERE user_ID = '$user_id'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
//Album Informationen ziehen herausfinden
$sql = "SELECT project_name, sticker_count, manufacturer, sport, creation_datetime FROM project_db WHERE project_id = '$row[project_id]'";
$result2 = $conn->query($sql);
if ($result2->num_rows > 0) {
// output data of each row
while($row2 = $result2->fetch_assoc()) {
//OUTPUT
echo $row2['project_name'] . ' '
$row2['count'] . ' '
$row2['sport'] . ' '
$row2['manufacturer'] . ' '
$row2['creation_datetime'];
}
}
}
}
Note putting arrays in your string doesn't work, so i have concatenated the echo instead.

Using mysql_fetch_assoc and updating each record

Some of my rows have a "," comma as the initial character in the field. So I need to loop through, check if each row has the initial commma, remove it if it does, and update the row.
I am running the following code, which seems to go on an endless loop when the update is called.
When I am just echoing out the result at the end, everything looks fine in the browser. But on execution of the update line below the echo, it seems as if a single datum from the column "Tags" is being populated for every record, instead of just the rows that have the initial commma that I am removing.
Would love help :)
$query = mysql_query("SELECT Tags FROM products")
or die (mysql_error());
while ($row = mysql_fetch_assoc($query))
{
$str = $row['Tags'];
$initial = substr($str,0,1);
if ($initial = ",") {
$str = (ltrim($str, ','));
}
echo "result: " .$str . "<br/>";
$result = "UPDATE products SET Tags = '" .$str ."'";
mysql_query($result);
}
Thank you.
You should pass the particular row id to the one you're making changes to, by using a WHERE clause:
$query = mysql_query("SELECT Tags FROM products")
or die (mysql_error());
while ($row = mysql_fetch_assoc($query)) {
$str = $row['Tags'];
$initial = substr($str,0,1);
if ($initial == ",") {
// == not =
$str = (ltrim($str, ','));
}
$id = $row['id'];
echo "result: " .$str . "<br/>";
$result = "UPDATE products SET Tags = '$str' WHERE id = $id";
mysql_query($result);
}
By the way, if possible kindly change to the better extension which is mysqli or PDO instead.
You're if() statement has an error in it.
You're using one equal:
if($initial = ",") {
}
Instead of two for actual comparison:
if($initial == ",") {
}
Here is the complete code. Thank you everyone.
$query = mysql_query("SELECT ProductID, Tags FROM products")
or die (mysql_error());
while ($row = mysql_fetch_assoc($query)) {
$str = $row['Tags'];
$initial = substr($str,0,1);
if ($initial == ",") {
$str = (ltrim($str, ','));
$id = $row['ProductID'];
//echo $id . " ";
//echo $str . "<br/>";
$result = "UPDATE products SET Tags = '$str' WHERE ProductID = $id";
echo $result ."<br>";
mysql_query($result);
}
}
So grateful for the help. I will update to mysqlli also.

Select certain MySQL fields and display them using php

So what I want to do is I want to search for a certain name in a column called 'filename' and display the 'count' on that row.
This might help explain:
I want to search for Packages in the 'filename' column but I want to display the 'count' on it (4)
id filename count
1 Packages 4
2 Another 7
$filename = 'Packages';
$result = mysql_query("SELECT `count` FROM `downloads` WHERE `filename` = $filename");
Also note that if the filename can come from user input you have to be aware of injection attacks.
$filename = 'Packages'; // From user
$filename = mysql_real_escape_string($filename);
$result = mysql_query("SELECT `count` FROM `downloads` WHERE `filename` = $filename");
Here is an example that selects all Filenames and counts and lists them in a table:
$result = mysql_query("SELECT `filename` `count` FROM `downloads`");
?><table><tr><td>Filename</td><td>Count</td></tr><?php
while($row = mysql_fetch_assoc($result)){
?>
<tr><td><?php echo $row['filename']; ?></td><td><?php echo $row['count']; ?></td></tr>
<?php
}
?></table><?php
Assuming what you actually wants is a report of download counts for each filename, you need to use the GROUP BY clause in your SQL statement:
<?php
$res = mysql_query('SELECT id, filename, COUNT(*) AS count FROM downloads GROUP BY filename');
if (is_resource($res)) {
while (false !== ($row = mysql_fetch_assoc($res))) {
printf('%d - %s - %d', $row['id'], $row['filename'], $row['count']);
}
}
?>
$query = "SELECT count FROM downloads WHERE filename='Packages'";
EDIT:
$result = mysql_query($query, $con) or die(mysql_error());
$arr_down = mysql_fetch_assoc($result);
echo $arr_down["count"];
UPDATE: For one or more rows with 'Packages' in it, you can show the results in a table like this:
$con = mysql_connect("localhost", "MySQL_username", "MySQL_password") or die ("Unable to connect to MySQL Server " . mysql_error());
$fname = "Packages";
$query = "SELECT filename, count FROM downloads WHERE filename='" . $fname . "'";
$result = mysql_query($query, $con) or die(mysql_error());
$html = "<table>\n<tr><th>Filename</th><th>Count</th></tr>\n";
while ($row = mysql_fetch_assoc($result))
$html .= "<tr><td>" . $row['filename'] . "</td><td>" . $row['count'] . "</td></tr>\n";
$html .= "</table>\n";
echo $html;
$count = mysql_num_rows($result)
echo($count)
Check out the PHP mysql documentation for more. In particular you'll want to learn about the fetch() functions, which you'll use to display the data you retrieve with your query.
the raw query would be:
select count( filename ) from downloads;
Edit: Okay, you want to loop them onto the page:
foreach( $result as $i )
{
if( !empty( $i ) )
{
echo "<div>$i['column_name']</div>";
}
}

Issue getting num_rows with mysqli

I've got this code:
$query = 'SELECT * FROM users WHERE username = "' . $user . '" AND password = MD5("' . $pass . '") LIMIT 1';
echo $query;
$result = $mysqli->query($query);
echo "<br>Here: " . $result->num_rows;
Using the output from $query I put it into phpmyadmin and it returns 1 row but when trying to get the number of rows using num_rows it doesn't return anything at all - not even 0;
Any ideas?
http://ru2.php.net/manual/en/mysqli.query.php
Returns FALSE on failure.
Have you established a connectection to your DB?
Looks like the SQL query is messed up. As far as I know there is no MD5 function built into MySQL nor the SQL standard. Therefore if it does exist then it is probably proprietary to the particular DB.
That said you can use PHP to check it. Try this to see what is going on.
$encrypted_pass = md5($pass);
$query = "SELECT * FROM users WHERE username = '$user' AND password = '$encrypted_pass' LIMIT 1";
echo $query;
$result = $mysqli->query($query);
if ($result !== FALSE) {
echo "<br>Here: " . $result->num_rows;
}
else {
echo "Something is broken.";
}
Ref:
http://us2.php.net/manual/en/mysqli-result.num-rows.php

selecting row from mysql if id matches

I want to select a row from mysql which matches a specific id. I want to get the result if the ID matches, if the ID does not exists in the database, it should not do anything.
I run sth like this:
$q = "SELECT * FROM entries where id= '1'";
$result = mysql_query($q) or die(mysql_error());
if($result){
$row = mysql_fetch_array($result) or die(mysql_error());
$name = $row['name'];
}
echo "hello".$name;
If the id '1' exists in the db, it should get the name, otherwise nothing or at least it should give the error, but when i use this, it just display no any content of the page which comes after this code. What I'm doing wrong?
If it does not display any code after this code, this is probably due to an error occuring and your error handling being set so the error is not displayed.
Try searching for the php error log file (normaly php_error.log) that should contain the error that you do not see.
Another thing i would try is adding more echo statements to see where exactly php stops interpreting.
Like this:
$q = "SELECT * FROM entries where id= '1'";
$result = mysql_query($q);
echo '<br />Query is send';
if(!$result) {
die('<br/>MySQL Error: ' . mysql_error());
}
else {
echo '<br />Result is true';
$row = mysql_fetch_array($result);
echo '<br />tryed fetching row';
if ($row === FALSE) {
echo '<br />$row is not false.';
$name = $row['name'];
echo '<br />$name now is "' . $name . '"';
}
else {
die('<br/>MySQL Error: ' . mysql_error());
}
}
echo '<br />hello: "' . $name . '"';
That might help to get some more information about your problem.
$id = 1;
$sql = "SELECT `name` FROM `entries` WHERE `id` = $id LIMIT 1" //Since the id is probably an integer type on the DB, the single quotes aren't necessary, and sometimes screw it up. I think MySQL possibly thinks it's a string when in quotes.
$result = mysql_query($sql) or die(mysql_error());
if(mysql_num_rows($result))
{
$row = mysql_fetch_assoc($result) or die(mysql_error());
$name = $row['name'];
echo 'Hello ' . $name;
}
A SELECT query can return 0 rows if the condition you specified doesn't match any rows, and that isn't an error.
You should rather check the result of mysql_num_rows() after sending the query.
Maybe you should add a else statement to your if.
if($result)
....
else
do something
You might even want to do a try catch statement.

Categories