简体   繁体   中英

print php variable values with mysql query output

I need to print php variable values with mysql query output

eg:

in mysql sampleTable data stored as follows

cust_id=1 and cust_msg='Welcome to $myname portal and points earn $mypoint'

I need to print Welcome to Sha portal and points earn 100 when I hit http://localhost/print.php?name=Sha&point=100

but it's printing variable name instead of value

Welcome to $myname portal and points earn $mypoint

php code

$mypoint=$_GET['point'];
$myname=$_GET['name'];

$sql_get_print="select cust_msg from sampleTable where cust_id=1";
$result_get_print=mysqli_query($con, $sql_get_print);
$get_row_print =mysqli_fetch_row($result_get_print);
$get_print_msg_text=$get_row_print[0];
mysqli_free_result($result_get_print);
echo "$get_print_msg_text";

Output Result got Print:

Welcome to $myname portal and points earn $mypoint

My Requirement is to Print:

Welcome to Sha portal and points earn 100

Please let me know how to achieve this?

You need to use mysqli_fetch_assoc instead of mysqli_fetch_row

and access the value in this manner

echo $get_row_print["cust_msg"];

You are getting name and point in $_GET global variable then what is the need of query. According to your requirement you can achieve this by simple echo Like this.

$mypoint=$_GET['point'];
$myname=$_GET['name'];
echo "Welcome to ".$myname. "portal and points earn ".$mypoint;

If you print that string from db, you're going to print just what's in the string and NOT the variable values. I think you'll have to go with a str_replace before printing!

I would suggest you to start from this basic mysqli example to keep things easy:

// connection to your DB.
$con = new mysqli("your_host", "your_user", "your_password", "your_db");
if ($con->connect_error)
{
    die("Connection Error");
}

// Perform SQL Query
$sql = "select cust_msg from sampleTable where cust_id=1";

$query = $con->query($sql) or die("Query Error");

// Print results. 
while ($row= $query->fetch_assoc())  
{
    // for each resulting row (1 in your case, I guess)
    echo $row['cust_msg'];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM